Skip to content

Instantly share code, notes, and snippets.

@bolasblack
Created October 5, 2018 04:39
Show Gist options
  • Save bolasblack/c4fd9fcacfc577111b2eb2c1db52c92c to your computer and use it in GitHub Desktop.
Save bolasblack/c4fd9fcacfc577111b2eb2c1db52c92c to your computer and use it in GitHub Desktop.

Promise.race

const result = await Promise.race([promise1, promise2])
(go (let [[result chan] ;; result the value and while chan won
          (<! (alts! [chan1
                      [value chan2] ;; even can wait a value >! chan
                      ]))]
      #_(doSomething)))

Promise.all

const [ result1, result2 ] = await Promise.all([promise1, promise2])
(go (let [res ;; first (+ chan1-result chan2-result) result
          (<! (async/map
                (fn [chan1-result chan2-result]
                    ;; wait two channel all emitted value
                    ;; once one channel closed, the result channel also be closed
                    (+ chan1-result chan2-result))
                [chan1 chan2]))]
      #_(doSomething)))

async/into

(go (let [records (<! (async/into [] chan))]
      ;; records is the all result of chan
      (assert (vector? records))
      #_(doSomething))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment