这种“问题”与发电机本身无关,只是与发电机有关await
内部机制try..catch
块,因为每当一个 Promise 在 try-catch 块内被拒绝时,catch
已加入(除非 Promise 是单独的 try-catch)。
事实上,生成器无法再进一步,因为一旦catch
以某种方式达到,它将继续直到另一个yield
被调用。如果不需要调用任何一个,它就会完成给出done: true
,这就是生成器的预期行为。
您的主要问题是您期望生成器产生所有值,但它只是can't,因为yield
块是从未达到:
try {
const thing = await fetchThing()
const otherThing = await fetchAnother()
yield { // <-- never met if either thing or otherThing are rejected.
...thing,
...otherThing
}
} catch (error) { // <-- this block is reached whenever either thing or otherThing raise an exception.
console.log('Error happened, but thats ok, I want to continue')
}
如果你想要你的try..catch
如果任何一个内部可等待元素引发异常,则阻止继续,您还需要尝试捕获它们,以便可以进一步控制它们的“失败”行为:
try {
let thing, otherThing;
try {
thing = await fetchThing()
otherThing = await fetchAnother()
}
catch (innerException) {
console.log('either of the above failed!', innerException);
}
// in this way, the below block will be reached.
yield {
...thing,
...otherThing
}
} catch (error) {
console.log('Error happened, but thats ok, I want to continue')
}
这样一来,无论是双双失败,还是双双成功yield
将到达块并将继续执行。
这是一个显示上述内容的示例:
const fakeAsync = async () => await Promise.resolve(true);
const fakeAsyncReject = async () => await Promise.reject(false);
async function* getAll(someStuff) {
try {
let res, raiseExc;
try {
res = await fakeAsync();
}
catch (innerResException) {
console.log('an inner exception raised.');
}
try {
raiseExc = await fakeAsyncReject();
}
catch (innerRaiseException) {
console.log('an inner exception was raised.', innerRaiseException);
}
// yield block, always reached unless there is something really wrong in this try block, like syntax errors or whatever.
yield {
res,
raiseExc
}
}
catch (error) {
// catch block raised only when the try block above yields an exception, NOT raised when either of the try-catch blocks inside the try-catch actually join the catch.
console.log('Error happened', error);
}
}
// Uncomment this block if you want to see how the for-await would work.
/*
(async() => {
for await (var res of getAll([])) {
console.log('res is', res);
}
})();
*/
(async() => {
const asyncIterator = getAll([]);
console.log(await asyncIterator.next());
})();