Last active
March 3, 2017 16:07
-
-
Save Jberivera/eefe00dfb02a085287e8ee9cb26e49da 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
| function getDataPromise () { | |
| var random = Math.random() * 5000 | 0; | |
| return new Promise(function (resolve, reject) { | |
| setTimeout(function () { | |
| resolve(random); | |
| }, random); | |
| }); | |
| } | |
| function runner (generator) { | |
| var iterator = generator(); | |
| function onNext (next) { | |
| if (next.done) { | |
| return; | |
| } | |
| next.value.then(function (value) { | |
| onNext(iterator.next(value)); | |
| }); | |
| } | |
| onNext(iterator.next()); | |
| } | |
| function* showDataInOrder () { | |
| var r1 = yield getDataPromise(); | |
| console.log('r1: ', r1); | |
| var r2 = yield getDataPromise(); | |
| console.log('r2: ', r2); | |
| var r3 = yield getDataPromise(); | |
| console.log('r3: ', r3); | |
| } | |
| runner(showDataInOrder); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment