Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Last active April 24, 2016 16:21
Show Gist options
  • Save ernestlv/69648e48ed3c942565bed094e80a8acf to your computer and use it in GitHub Desktop.
Save ernestlv/69648e48ed3c942565bed094e80a8acf to your computer and use it in GitHub Desktop.
prints 1,2,3,4,5, 6 using promises at different times
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)
@ernestlv
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment