Last active
November 30, 2016 18:48
-
-
Save skyksandr/5cb29e34fe799513f842e52172e78fc4 to your computer and use it in GitHub Desktop.
Return Promise from then.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Execution flow for example below: | |
| // Promise-callback | |
| // |- then | |
| // |- then | |
| // |- Promise-callback | |
| // |- then | |
| // |- Promise-callback | |
| // |- then | |
| // So the answer on the question: | |
| // What will be if return new Promise from then? | |
| // Returned Promise chain will be finished before then from parent chain called. | |
| var a = new Promise(function(resolve, reject) { | |
| console.log('1') | |
| setTimeout(function() { | |
| console.log('2'); | |
| resolve(); | |
| }, 1000); | |
| }).then(function() { | |
| console.log('3'); | |
| }).then(function() { | |
| return new Promise(function(resolve, reject) { | |
| console.log('4') | |
| setTimeout(function() { | |
| console.log('5'); | |
| resolve(); | |
| }, 500); | |
| }).then(function() { | |
| return new Promise(function(resolve, reject) { | |
| setTimeout(function() { | |
| console.log('6'); | |
| resolve(); | |
| }, 1000) | |
| }); | |
| }); | |
| }).then(function() { | |
| console.log('7'); | |
| }).catch(function(err) { console.log(err); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment