Skip to content

Instantly share code, notes, and snippets.

@theleoborges
Last active August 29, 2015 14:20
Show Gist options
  • Save theleoborges/d7f4362ff97b5ebe8614 to your computer and use it in GitHub Desktop.
Save theleoborges/d7f4362ff97b5ebe8614 to your computer and use it in GitHub Desktop.
(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
@theleoborges
Copy link
Author

@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