我有一个异步生成器函数,该函数内部调用了几个可能引发错误的异步函数。我想要的是,当发生错误时,生成器只会记录它,然后继续进行进一步的工作。所以我有这样的代码...
async * getAll (somestuff) {
try {
const thing = await fetchThing()
const otherThing = await fetchAnother()
yield {
...thing,
...otherThing
}
} catch (error) {
console.log('Error happened, but thats ok, I want to continue')
}
}
但是,当发生错误时,它会被catch块记录下来,但随后生成器会产生{done:true}并停止操作。 我尝试在catch块中的console.log之后手动产生null,但结果相同。
答案 0 :(得分:2)
这种“问题”与生成器本身无关,而仅与await
块内的try..catch
机制有关,因为只要在try-中拒绝了诺言, catch块,catch
就加入了(除非诺言是分开尝试捕获的)。
实际上,生成器无法继续前进,因为一旦以某种方式到达了catch
,它将继续直到调用另一个yield
为止。如果不需要调用任何内容,则只需结束提供done: true
,这就是生成器的预期行为。
您的主要问题是,您期望生成器产生所有值,但无法,因为yield
块从未达到 strong>:
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());
})();