Skip to content

Instantly share code, notes, and snippets.

@randylien
Forked from shaunlebron/es7coreasync.md
Created July 10, 2017 02:16
Show Gist options
  • Save randylien/576dae17f2bda66a28c31bc78d614268 to your computer and use it in GitHub Desktop.
Save randylien/576dae17f2bda66a28c31bc78d614268 to your computer and use it in GitHub Desktop.
es7 vs core.async

Comparing ES7 and core.async

ES7 core.async
async function() {...} (fn [] (go ...))
await ... (<! ...)
await* or Promise.all(...) (doseq [c ...] (<! c))

async

Calling foo returns a promise receiving value 1:

async function foo () {
  return 1;
}

Calling foo returns a channel receiving value 1:

(defn foo []
  (go 1))

await

bar is parked until foo has finished:

async function bar() {
  await foo();
}

bar's go-block is parked until foo has finished:

(defn bar []
  (go
    (<! (foo))))

await all

baz waits for foo and bar to complete:

async function baz() {
  await Promise.all([foo(), bar()]);
}
(defn baz []
  (go
    (doseq [c [(foo) (bar)]]
      (<! c))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment