Skip to content

Instantly share code, notes, and snippets.

@jesuscast
Last active July 31, 2017 23:06
Show Gist options
  • Save jesuscast/d7b9d0a29dd921e03d932c117eeba8ce to your computer and use it in GitHub Desktop.
Save jesuscast/d7b9d0a29dd921e03d932c117eeba8ce to your computer and use it in GitHub Desktop.

Revisions

  1. jesuscast revised this gist Jul 31, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion resolve_util.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,6 @@ function promiseReducer(arrayOfPromiseHolders, resolve, reject) {
    * resolves them in series
    *
    * Usage:
    *
    * resolveInSeries([
    * () => {
    * return new Promise((resolve) => { console.log("I will print first!"); resolve(); })
  2. jesuscast created this gist Jul 31, 2017.
    37 changes: 37 additions & 0 deletions resolve_util.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    function promiseReducer(arrayOfPromiseHolders, resolve, reject) {
    if (arrayOfPromiseHolders.length > 0) {
    const promiseHolder = arrayOfPromiseHolders.shift();
    promiseHolder().then(() => {
    promiseReducer(arrayOfPromiseHolders, resolve, reject);
    }).catch((err) => {
    reject(err);
    });
    } else {
    resolve();
    }
    }

    /**
    * Resolve in series takes an array of functions that return a promise, and then
    * resolves them in series
    *
    * Usage:
    *
    * resolveInSeries([
    * () => {
    * return new Promise((resolve) => { console.log("I will print first!"); resolve(); })
    * },
    * () => {
    * return new Promise((resolve) => { console.log("I will print second!"); resolve(); })
    * }
    * ]).then(() => {
    * console.log("I will print at the end");
    * });
    */
    function resolveInSeries(arrayOfPromiseHolders) {
    return new Promise((resolve, reject) => {
    promiseReducer(arrayOfPromiseHolders, resolve, reject);
    });
    }

    export resolveInSeries;