Optimize async to generator (#8267)

* Optimize async to generator

We can reuse the step function, avoiding a closure on every invocation.

* Rename step
This commit is contained in:
Justin Ridgewell
2018-07-05 10:51:16 -04:00
committed by GitHub
parent db1385fb82
commit e229ebbb8a
8 changed files with 43 additions and 24 deletions

View File

@@ -238,30 +238,35 @@ helpers.asyncGeneratorDelegate = () => template.program.ast`
`;
helpers.asyncToGenerator = () => template.program.ast`
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
export default function _asyncToGenerator(fn) {
return function () {
var self = this, args = arguments;
return new Promise(function (resolve, reject) {
var gen = fn.apply(self, args);
function step(key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
function _next(value) { step("next", value); }
function _throw(err) { step("throw", err); }
_next();
_next(undefined);
});
};
}