Skip to content

Instantly share code, notes, and snippets.

@kejiweixun
Last active July 22, 2018 08:54
Show Gist options
  • Save kejiweixun/44e26ac939c99c69801c4a7726227698 to your computer and use it in GitHub Desktop.
Save kejiweixun/44e26ac939c99c69801c4a7726227698 to your computer and use it in GitHub Desktop.

Revisions

  1. kejiweixun revised this gist Jul 22, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions understanding_js_promise.js
    Original file line number Diff line number Diff line change
    @@ -45,3 +45,4 @@ console.log(result);
    //5
    //3
    //6
    //[ 'c', 'b', 'a' ]
  2. kejiweixun revised this gist Jul 22, 2018. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion understanding_js_promise.js
    Original file line number Diff line number Diff line change
    @@ -34,4 +34,14 @@ a().then(function(result){
    console.log(result);
    });

    console.log(result);
    console.log(result);

    // -1
    //0
    //1
    //9
    //4
    //2
    //5
    //3
    //6
  3. kejiweixun created this gist Jul 22, 2018.
    37 changes: 37 additions & 0 deletions understanding_js_promise.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    let result = 9;
    function a(){
    console.log(-1)
    return new Promise(function(resolve, reject){
    console.log(0)
    resolve('a');
    console.log(1)
    });
    };

    function b(message){
    return new Promise(function(resolve, reject){
    console.log(2)
    resolve('b' + message);
    });
    };

    function c(message){
    return new Promise(function(resolve, reject){
    console.log(3)
    resolve('c' + message);
    });
    };

    a().then(function(result){
    console.log(4)
    return b(result);
    }).then(function(resultFromB){
    console.log(5)
    return c(resultFromB);
    }).then(function(resultFromC){
    console.log(6);
    [...result] = resultFromC;
    console.log(result);
    });

    console.log(result);