Created
December 28, 2016 04:27
-
-
Save thiagoh/5ab2977e95917f290c71184aa2875fad to your computer and use it in GitHub Desktop.
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
var createPromise = function(label, time) { | |
return function() { | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log(label); | |
resolve(); | |
}, time); | |
}); | |
}; | |
}; | |
new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log('A'); | |
resolve(createPromise('B', 900)()); // this dispatches the promise right away | |
}, 500); | |
}) | |
.then(createPromise('C', 100)) // this DO NOT dispatches the promise right away | |
.then(createPromise('D', 10)) | |
.then(createPromise('E', 30)) | |
.then(createPromise('F', 5)); | |
$ node promises-2.js | |
A | |
B | |
C | |
D | |
E | |
F |
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
var createPromise = function(label, time) { | |
return new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log(label); | |
resolve(); | |
}, time); | |
}); | |
}; | |
new Promise(function(resolve) { | |
setTimeout(function() { | |
console.log('A'); | |
resolve(createPromise('B', 900)); | |
}, 500); | |
}) | |
.then(function() { | |
return createPromise('C', 100); | |
}) | |
.then(createPromise('D', 10)) // this dispatches the promise right away | |
.then(createPromise('E', 30)) // this dispatches the promise right away | |
.then(function() { | |
return createPromise('F', 5); | |
}); | |
$ node promises-1.js | |
D | |
E | |
A | |
B | |
C | |
F |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment