gpt4 book ai didi

javascript - 尽管被捕获,但生成器内部抛出的错误完成了它

转载 作者:行者123 更新时间:2023-11-30 13:55:04 24 4
gpt4 key购买 nike

我有一个异步生成器函数,它在内部调用了几个可能引发错误的异步函数。我想要的是,当发生错误时,生成器只是记录它,然后继续进一步工作。所以我有这样的代码...

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 block 记录下来,但随后生成器会产生 { done: true } 并且操作停止。我尝试在 catch block 中的 console.log 之后手动生成 null,但结果相同。

最佳答案

这种“问题”与生成器本身无关,它只是与 try..catch block 中的 await 机制有关,因为 每当一个 promise 在 try-catch block 中被拒绝时,catch 就会加入(除非 promise 是单独 try catch 的)。

事实上,生成器不能再继续了,因为一旦以某种方式到达catch,它将继续直到调用另一个yield。如果不需要调用任何东西,它就完成给出 done: true,这就是生成器的预期行为。

您的主要问题是您希望生成器产生所有值,但它不能,因为yield block 从不达到:

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 block 在任一内部可等待元素引发异常时继续,您还需要 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 block 并继续执行。

下面是一个例子,展示了上面所说的内容:

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());
})();

关于javascript - 尽管被捕获,但生成器内部抛出的错误完成了它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57458966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com