Use of async for Get, GetJSON, etc. calls

125 views
Skip to first unread message

Dmitri Nesteruk

unread,
Apr 2, 2011, 4:12:58 AM4/2/11
to websh...@googlegroups.com
I have a HTML Sitelet app that uses JQuery to consume JSONP from RESTful services. Typically my code looks like the following:

JQuery.GetJSON(url, fun x y -> process(x))

I was wondering if it was possible to somehow coerce WebSharper to use the async notation here.
What I mean is, I'd much prefer to write

async {
  let! (x, y) = JQuery.GetJSON(url)
}

Just curious if this is somehow possible or whether this sort of thing is planned.

Vladimir Matveev

unread,
Apr 2, 2011, 4:36:18 PM4/2/11
to websh...@googlegroups.com

Yes, this is possible, something like this:

 

    [<AutoOpen>]
    module JsonExtensions = 

        open IntelliFactory.WebSharper.JQuery
        type JQuery with
            [<JavaScript>]
            static member GetJsonAsync(url : string, ?data : obj) = 
                Async.FromContinuations <|
                    fun (ok, _, _) ->
                        let cont (r : obj, _ : string) = ok (r :?> _)
                        match data with
                        | Some d -> JQuery.GetJSON(url, d, cont)
                        | None -> JQuery.GetJSON(url, cont)
                        |> ignore

 

//…

        [<JavaScript>]
        override this.Body =
            Button [Text "Click"]
            |>! OnClick (fun _ _ ->
                async {
                    let! (res : string) = JQuery.JQuery.GetJsonAsync("http://api.twitter.com/1/help/test.json?callback=?" 
)
                    JavaScript.Alert(res)                        
                }
                |> Async.Ignore
                |> Async.StartImmediate
            ) :> _

Dmitri Nesteruk

unread,
Apr 4, 2011, 1:25:41 PM4/4/11
to websh...@googlegroups.com
Thanks! However, I'm not sure I understand how to catch errors in this one. Seems like the second parameter to 'cont' is just thrown away...

Zakaluka

unread,
Apr 1, 2012, 8:31:00 PM4/1/12
to websh...@googlegroups.com
Sorry to resurrect an old thread (I'm sure people have already found the solution), but here's what I came up with:

[<AutoOpen>]
module JQueryExt =
  open IntelliFactory.WebSharper.JQuery
  type JQuery with
    [<JavaScript>]
    static member GetJsonAsync(url : string, ?data : obj) =
      Async.FromContinuations <|
        fun (ok, _, _) ->
          let cont (retData : obj, status : string) = ok (retData, status)

          match data with
          | Some d -> JQuery.GetJSON(url, d, cont)
          | None -> JQuery.GetJSON(url, cont)
          |> ignore

...

      [<JavaScript>]
      override this.Body =
        Button [Text "what?"]
          |>! OnClick (fun elem args ->
            async {
              let! ret, status = JQuery.GetJsonAsync("http://api.twitter.com/1/help/test.json?callback=?")
              JavaScript.Alert(ret.ToString() + "\n" + status)
            }
            |> Async.Ignore
            |> Async.Start
          )
        :> IPagelet

Even though the 'ok' continuation is constrained from 'a to (obj * string), that should be okay because the success callback method for GetJSON has that argument as its input signature.

Did anyone else come up with anything similar?

Regards,

z.
Reply all
Reply to author
Forward
0 new messages