Skip to content

Instantly share code, notes, and snippets.

@iamso
Last active March 21, 2017 12:24
Show Gist options
  • Select an option

  • Save iamso/0983e547b7b007d821bd95f01fe15890 to your computer and use it in GitHub Desktop.

Select an option

Save iamso/0983e547b7b007d821bd95f01fe15890 to your computer and use it in GitHub Desktop.

Revisions

  1. iamso revised this gist Mar 21, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion promise-chain.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@

    // the array to loop through
    const arr = [0,1,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
    const arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];

    // start with a resolved promise
    let p = Promise.resolve();
  2. iamso revised this gist Mar 21, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions promise-chain.js
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,10 @@ for (let i of arr) {

    // create promise chain
    p = p.then(() => {

    // return the promise for chaining
    return functionWithPromise(i);

    });

    }
    @@ -27,6 +29,7 @@ function functionWithPromise(text) {

    // simulate asyncronous activity
    setTimeout(() => {

    // end the function
    console.log(`end promise: ${text}`);

  3. iamso created this gist Mar 21, 2017.
    40 changes: 40 additions & 0 deletions promise-chain.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@

    // the array to loop through
    const arr = [0,1,3,4,5,6,7,8,9,10,11,12,13,14,15,16];

    // start with a resolved promise
    let p = Promise.resolve();

    // loop through the array
    for (let i of arr) {

    // create promise chain
    p = p.then(() => {
    // return the promise for chaining
    return functionWithPromise(i);
    });

    }

    // dummy function with promise
    function functionWithPromise(text) {

    // return a promise
    return new Promise((resolve, reject) => {

    // start the function
    console.log(`start promise: ${text}`);

    // simulate asyncronous activity
    setTimeout(() => {
    // end the function
    console.log(`end promise: ${text}`);

    // resolve the promise
    resolve(text);

    }, 1000);

    });

    }