futura.promise
A promise implementation for Clojure that uses jdk8 completable futures behind the scenes.
all
(all promises)
Given an array of promises, return a promise that is resolved when all the items in the array are resolved.
any
(any promises)
Given an array of promises, return a promise that is resolved when first one item in the array is resolved.
deliver
(deliver p)
(deliver p v)
Mark the promise as completed or rejected with optional value.
If value is not specified nil
will be used. If the value is instance of Throwable
the promise will be rejected.
promise
(promise)
(promise v)
A promise constructor.
This is a polymorphic function and this is a list of possible arguments:
- throwable
- plain value
- factory function
In case of the initial value is instance of Throwable
, rejected promise will be retrned. In case of a plain value (not throwable), a resolved promise will be returned. And finally, if a function or any callable is provided, that function will be executed with one argument as callback for mark the promise resolved or rejected.
(promise (fn [deliver]
(future
(Thread/sleep 200)
(deliver 1))))
The body of that function can be asynchronous and the promise can be freely resolved in other thread.
reason
(reason p)
Get the rejection reason of this promise. Throws an error if the promise isn’t rejected.