blackbird icon indicating copy to clipboard operation
blackbird copied to clipboard

Is there a way to block cl-async-repl until a promise is finished?

Open malisper opened this issue 8 years ago • 2 comments

Hi,

I am trying to use the cl-async-repl library in combination with the drakma-async library. One issue I've ran into is that it seems impossible to return the result of a promise as the result of the repl. What I am looking for is a function (which I am going to call force) which takes a promise and blocks until the promise is finished, and then returns the value inside of the promise. As an example, when used within cl-async-repl

(force (drakma-async:http-request ...))

should act like ordinary drakma:http-request.

Does such a function already exist? If not, is there an easy way to emulate its behavior? The closest thing I've come up with is to add a callback to the promise that will print out the value, but I am looking for something that actually returns the value.

malisper avatar Feb 29 '16 09:02 malisper

Paging @ivan4th for this one...I don't have as great of an understanding of the REPL system, since he built it into cl-async.

orthecreedence avatar Mar 05 '16 21:03 orthecreedence

I just started playing with blackbird and was surprised there was no force function. I'm just on the slime repl and using promises on threaded tasks, not using cl-async-repl. Currently I make do with:

(defun force (promise &key (poll-interval 0.1))
  (loop until (blackbird:promise-finished-p promise)
        do (sleep poll-interval)
        finally (return (slot-value promise 'blackbird::values))))

I was also surprised that there wasn't a function to ask a promise for its value (and maybe error out if the promise isn't finished), hence the slot-value call. I would use a thread sync primitive but figure promise related functions shouldn't know that the promise is waiting on a thread.

spacebat avatar Jul 29 '16 07:07 spacebat