Last active
August 29, 2015 14:22
-
-
Save dweinstein/14947f8e79afd580664c to your computer and use it in GitHub Desktop.
async with generators / 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
// http://pag.forbeslindesay.co.uk/#/22 | |
function async(makeGenerator){ | |
return function (){ | |
var generator = makeGenerator.apply(this, arguments) | |
function handle(result){ // { done: [Boolean], value: [Object] } | |
if (result.done) return result.value | |
return result.value.then(function (res){ | |
return handle(generator.next(res)) | |
}, function (err){ | |
return handle(generator.throw(err)) | |
}) | |
} | |
return handle(generator.next()) | |
} | |
} |
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
// http://pag.forbeslindesay.co.uk/#/28 (parallel operations) | |
var get = async(function *(){ | |
var left = yield readJSON('left.json') | |
var right = yield readJSON('right.json') | |
return {left: left, right: right} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment