I was just reading this fantastic article «Generators» and it clearly highlights this function, which is a helper function for handling generator functions:
function async(makeGenerator){
return function () {
var generator = makeGenerator.apply(this, arguments);
function handle(result){
// result => { done: [Boolean], value: [Object] }
if (result.done) return Promise.resolve(result.value);
return Promise.resolve(result.value).then(function (res){
return handle(generator.next(res));
}, function (err){
return handle(generator.throw(err));
});
}
try {
return handle(generator.next());
} catch (ex) {
return Promise.reject(ex);
}
}
}
which I hypothesize is more or less the way the async
keyword is implemented with async
/await
. So the question is, if that is the case, then what the heck is the difference between the await
keyword and the yield
keyword? Does await
always turn something into a promise, whereas yield
makes no such guarantee? That is my best guess!
You can also see how async
/await
is similar to yield
with generators in this article where he describes the 'spawn' function ES7 async functions.
async/await
is not part of ES7. Please read tag description. – Jokjakarta