-
-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
await async 如何实现 #280
Comments
function* generatorFunc() {
const data1 = yield getData()
console.log('data1', data1);
const data2 = yield getDataTwo()
console.log('data2', data2);
}
// 自动执行
function autoGenerateFunc(generatorFunc) {
return function () {
// 生成迭代器
const gen = generatorFunc.apply(this, arguments)
return new Promise((resolve, reject) => {
const step = (p, arg) => {
let genObj;
try {
genObj = gen[p](arg)
} catch (error) {
return reject(error)
}
const { value, done } = genObj;
if (done) {
return resolve(value)
} else {
return Promise.resolve(value).then(val => step('next', val), err => step('throw', err))
}
}
step('next')
})
}
} |
let getData = () => { |
|
源码来源
The text was updated successfully, but these errors were encountered: