Skip to content

Instantly share code, notes, and snippets.

@skyksandr
Last active November 30, 2016 18:48
Show Gist options
  • Select an option

  • Save skyksandr/5cb29e34fe799513f842e52172e78fc4 to your computer and use it in GitHub Desktop.

Select an option

Save skyksandr/5cb29e34fe799513f842e52172e78fc4 to your computer and use it in GitHub Desktop.
Return Promise from then.
// 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