Skip to content

Instantly share code, notes, and snippets.

@thiagoh
Created December 28, 2016 04:27
Show Gist options
  • Save thiagoh/5ab2977e95917f290c71184aa2875fad to your computer and use it in GitHub Desktop.
Save thiagoh/5ab2977e95917f290c71184aa2875fad to your computer and use it in GitHub Desktop.
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
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