Created
June 17, 2018 07:15
-
-
Save m8r1x/32b3030556a6f970a7b8f60958a2ab16 to your computer and use it in GitHub Desktop.
Promises + Generators
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 async(generatorFn) { | |
return function () { | |
var iterator = generatorFn.apply(this, arguments); | |
function handle(result) { | |
if (result.done) return Promise.resolve(result.value); | |
return Promise.resolve(result.value).then(function (res) { | |
return handle(iterator.next(res)); | |
}, function (err) { | |
return handle(iterator.throw(err)); | |
}); | |
} | |
try { | |
return handle(iterator.next()); | |
} catch (ex) { | |
return Promise.reject(ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment