Last active
August 29, 2015 14:20
-
-
Save theleoborges/d7f4362ff97b5ebe8614 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
(async | |
(let [a (await (async-task-a)) | |
b (await (async-task-b)) | |
c (await (async-task-c (* a b)))] | |
(+ a b c))) | |
;; whish is equivalent to: | |
(async | |
(let [a (await (async-task-a)) | |
b (await (async-task-b))] | |
(+ a | |
b | |
(await (async-task-c (* a b)))))) | |
;; async blocks return futures |
This is quite nice!
@jed,
I've updated the example to perhaps make it slightly clearer. The two are equivalent and are an alternative way of writing flatmap/map;
(flatmap (async-task-a)
(fn [a]
(flatmap (async-task-b)
(fn [b]
(map (async-task-c (* a b))
(fn [c]
(+ a b c)))))))
Or this do-notation:
(do [a (async-task-a)
b (async-task-b)
c (async-task-c (* a b))]
(return (+ a b c)))
It just allows you to write sync-looking async code without necessarily having to bind the result of intermediate values if it's not necessary. (in haskell one could also use liftA to the same effect).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you describe what this does?
it seems to create an async-task that simply awaits the first two, and then awaits that. if so, that seems like an undesirable thing to want to do, so I assume this does something else entirely :-)