Created
May 21, 2016 23:09
-
-
Save mildfuzz/9120db7571cc8c58abfb1ef41de0cc95 to your computer and use it in GitHub Desktop.
Using a generator to trigger a sequence of promises
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
| function* triggerGen(res, rej) { | |
| const result = yield; | |
| if (result) { | |
| res(result); | |
| } else { | |
| rej(); | |
| } | |
| } | |
| var trigger; | |
| const prom = new Promise((res, rej) => { | |
| trigger = triggerGen(res, rej); | |
| trigger.next(); | |
| }); | |
| prom | |
| .then(function(x) { | |
| console.log(x); | |
| return Promise.resolve(x * 2) | |
| }) | |
| .then(function(y) { | |
| console.log(y); | |
| return Promise.resolve(y * 2); | |
| }) | |
| .then(function(z) { | |
| console.log(z * 2); | |
| }); | |
| setTimeout(function(){ | |
| trigger.next(2); | |
| },1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment