Skip to content

Instantly share code, notes, and snippets.

@dimpiax
Last active September 19, 2017 16:23
Show Gist options
  • Save dimpiax/ef207b3eabaebf2540d1f9183f0374ed to your computer and use it in GitHub Desktop.
Save dimpiax/ef207b3eabaebf2540d1f9183f0374ed to your computer and use it in GitHub Desktop.

Revisions

  1. dimpiax revised this gist Sep 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion promiseUtils.js
    Original file line number Diff line number Diff line change
    @@ -24,5 +24,5 @@ const result = await sync([1000, 500], async (value) => {
    return res
    })
    console.log(result) // [2000, 500] after 1500 ms
    console.log(result) // [2000, 1000] after 1500 ms
    */
  2. dimpiax revised this gist Sep 19, 2017. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions promiseUtils.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    /* @flow */

    const next = async (it, callback) => {
    const { value, done } = it.next()
    if (done) return []
  3. dimpiax created this gist Sep 19, 2017.
    30 changes: 30 additions & 0 deletions promiseUtils.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    /* @flow */

    const next = async (it, callback) => {
    const { value, done } = it.next()
    if (done) return []

    const result = await callback(value)
    const nextResult = await next(it, callback)
    return [result, ...nextResult]
    }

    const sync = async (value, callback) => {
    const it = value[Symbol.iterator]()
    const res = await next(it, callback)
    return res
    }

    export default {
    sync
    }

    // Usage:
    /**
    const result = await sync([1000, 500], async (value) => {
    const res = await new Promise((res) => { setTimeout(res, value, value * 2) })
    return res
    })
    console.log(result) // [2000, 500] after 1500 ms
    */