Open Rain120 opened 2 years ago
function asyncToGenerator(generatorFunction) {
return function () {
const gen = generatorFunction.apply(this, arguments);
return new Promise((resolve, reject) => {
function step(key, ...args) {
let generatorResult = null;
try {
generatorResult = gen[key](...args);
} catch (e) {
return reject(e);
}
const {value, done} = generatorResult;
if (done) {
resolve(value);
} else {
return Promise.resolve(value).then(
val => step('next', val),
err => step('throw', err)
);
}
}
step('next');
});
};
}
题目