Open shfshanyue opened 4 years ago
公众号里这篇文章留的 github 网址有误,那个 url 打开 404 :)
参考 @bebel/runtime
的实现代码如下,可在 asyncToGenerator.js 查看源代码
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 _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
赞赞赞。
(function (done) {
if (!done) return;
const getData = () => {
return new Promise((resolve) => setTimeout(() => resolve("data"), 1000));
};
function* testG() {
// await被编译成了yield
const data = yield getData();
console.log("data: ", data);
const data2 = yield getData();
console.log("data2: ", data2);
return "success";
}
function genratorWarp(testG) {
return new Promise((resolve, reject) => {
let it = testG();
function next(val) {
let { value, done } = it.next(val);
if (done) {
resolve(value);
} else {
Promise.resolve(value).then((data) => {
next(data);
}, reject);
}
}
next();
});
}
genratorWarp(testG).then((data) => {
console.log(data);
});
})(1);
function _awaiter(fn) {
let resolveFn = (value) => value
let rejectFn = (reason) => reason
let generator = fn
let promise = new Promise((resolve, reject) => {
resolveFn = resolve
rejectFn = reject
})
const adopt = (value) => {
return value instanceof Promise ? value : Promise.resolve(value)
}
const step = (preValue) => {
try {
const { value, done } = generator.next(preValue)
adopt(value).then(
(nextValue) => {
if (done) {
resolveFn(nextValue)
} else {
step()
}
},
(reason) => {
if (done) {
rejectFn(reason)
} else {
step()
}
}
)
} catch (err) {
rejectFn(err)
}
}
step()
return promise
}
参考
@bebel/runtime
的实现代码如下,可在 asyncToGenerator.js 查看源代码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 _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
请问_throw是否有必要给? 还有var gen = fn.apply(self, args); 是否有必要确定this指向,这里的self应该指向全局global or window.