On Sun, Dec 19, 2010 at 3:36 AM, HiHeelHottie <hiheelhot...@gmail.com> wrote:
>
> In Joy of Clojure, there is a callback API to blocking API example in
> the section on promises.  Chouser outlines it a briefly in a
> discussion on Promise/Deliver use cases here -
> http://groups.google.com/group/clojure/browse_thread/thread/b1548aa40ba8072/210ec81bfe26032e?lnk=gst&q=promise#210ec81bfe26032e:
>
> I've used them to convert a callback-based (continuation-passing
> style, if you will) API into a blocking one.  The lib I was using
> provides something you can call like:
>        (rpc-call destination method-args done)
> Where 'done' is a function that gets called with the results of
> the remote procedure call.  But I want to write a function that
> does rpc but *returns* the result, so...
>        (let [p (promise)]
>          (rpc-call destination method-args #(deliver p %))
>         �...@p)
> This will work just fine whether rpc-call calls 'done'
> synchronously, or if it returns right away and 'done' is called
> by some other thread later.
> --Chouser
>
> This is very neat.  Is this robust as is or would you need to add code
> to handle error cases such as an unavailable rpc server, rpc server
> never returning, an error trying to make the rpc call, etc.

It depends entirely on how rpc-call handles error cases.  If it
promises to always call its call back with something, even on
error cases (perhaps passing in an Error object or something),
then this would sufficient as-is.

However, anything that would cause the call back to be skipped
will of course cause the deliver to also be skipped, and the
deref on the promise will block forever.  The easiest way for
this to happen accidentally is if an exception is thrown, so
catching those and turning them into calls to the callback with
an error object is certainly a good idea.

--Chouser
http://joyofclojure.com/

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to