Last active
April 24, 2016 16:21
-
-
Save ernestlv/69648e48ed3c942565bed094e80a8acf to your computer and use it in GitHub Desktop.
prints 1,2,3,4,5, 6 using promises at different times
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
p1 = function(r) { | |
console.log(new Date(), r); | |
p2 = new Promise(r => setTimeout(x => r(3), 3000)); | |
p3 = new Promise(r => setTimeout(x => r(4), 7000)); | |
p4 = new Promise(r => setTimeout(x => r(5), 2000)); | |
s = Promise.resolve(); | |
s2 = s.then(x => p2); | |
s3 = s2.then(v => console.log(new Date(), v)); | |
s4 = s3.then(x => p3 ); | |
s5 = s4.then(v => console.log(new Date(), v)); | |
s6 = s5.then(x => p4 ); | |
s7 = s6.then(v => console.log(new Date(), v)); | |
return s7; | |
}; | |
p = new Promise(r => setTimeout(x => r(2), 1000)); | |
z = p.then(p1); | |
z.then(x => console.log(new Date(), 6)); | |
console.log(new Date(), 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see https://gist.github.com/ernestlv/a22eb8cafbff0a2f0561cb5ec4d7df38