While reading about asyncand awaitI noticed that this is almost the equivalent of generator functions. Consider this snippet from TypeScript Deep Dive :
Waiting for Async
(...)
async function foo() {
try {
var val = await getMeAPromise();
console.log(val);
}
catch(err) {
console.log('Error: ', err.message);
}
}
(...)
JavaScript generated
You do not have to understand this, but it is quite simple if you read about generators. A function foocan simply be wrapped as follows:
const foo = wrapToReturnPromise(function* () {
try {
var val = yield getMeAPromise();
console.log(val);
}
catch(err) {
console.log('Error: ', err.message);
}
});
where it wrapToReturnPromisesimply performs the function of a generator to receive generatorand then use generator.next(), if the value is equal promise, it will be then+ a catchpromise and, depending on the result, call genertor.next(result)or genertor.throw(error). What is it!
, " "? wrapToReturnPromise - , JavaScript.
: , "", "". .