Last active
June 12, 2017 18:41
-
-
Save remexre/e8b8f5ae2aef47ebb18473e43b40335e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn-async foo (bar) | |
; x and y are "awaited" in parallel | |
; This is the equivalent of Promise.all in JS. | |
; Note: this means x *cannot* be used in y. | |
(await (x (some-async-function)) | |
(y (other-async-function bar))) | |
; async-return is Promise.resolve in JS-speak, or the | |
; monad `return` function in Haskell-speak. | |
(async-return (+ x y))) | |
(defn-async quux () | |
; This is how you use an async result in another async function. | |
(await (x (yet-another-async-function))) | |
(await (y (other-async-function x))) | |
(async-return (+ x y))) | |
(defn-async xyzzy () | |
; async tail calls ofc allowed "raw" | |
(await (x (some-async-function))) | |
(last-async-function x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes:
defn-async
is a macro that handlesawait
properly.await
is only "defined" insidedefn-async
blocks.await
as a function name inside adefn-async
will cause fun.lambda
is todefn
asasync
is todefn-async
async
lambda-async
instead, just to make it more explicit.Either
/Result
is brought into the picture...Async
is basically a monad with an additional(Async a, Async b) -> Async (a, b)
operationEither
/Result
is definitely a monadlambda-monads
/defn-monads
form would make this simpler...(Monad m1, Monad m2) => m1 (m2 a) -> m2 (m1 a)
, so we can't doAsync (Either err ok) -> Either (Async err) (Async ok)
without knowing whether theEither
is an error or not, and since we're not a lazy language, that basically would involve evaluating the whole thing