Created
June 23, 2016 14:02
-
-
Save alemures/2ef056a1d6b0ba7af34329b16174adb9 to your computer and use it in GitHub Desktop.
Yield Promises
This file contains 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 run(generator) { | |
var it = generator(); | |
var obj = it.next(); | |
(function _run() { | |
if (!obj.done) { | |
obj.value.then(result => { | |
obj = it.next(result); | |
_run(); | |
}).catch(err => it.throw(err)); | |
} | |
}()); | |
} | |
// Example of usage | |
run(function*() { | |
try { | |
var app = yield getApp(12312); | |
var user = yield getUser(23123); | |
console.log(app, user); | |
} catch(e) { | |
console.error(e); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment