会发生什么:如果我使用 fetch..catch 并调用另一个函数。在下一个函数中,如果有任何崩溃。它会被 last catch 捕获。这将继续下去,所以如果下一个函数崩溃,它仍然会在 fetch..catch 中被捕获
我想要什么:在调用 myExternalFunction() 时,我想从 fetch 抛出的 try..catch 中“断开连接”。
fetch('mystuff.url')
.then((data)=>{
myExternalFunction();
})
.catch((e)=>{
// all future errors will come here
})
myExternalFunction(){
// This error will be caught by fetch..catch
// we don't want that
crash();
}
最佳答案
您不想链接您的catch
到then
then,但要将它们保持在同一级别:
fetch(...).then(successCallback, failCallback)
或者:
const p = fetch(...);
p.then(successCallback);
p.catch(failCallback);
const p = Promise.resolve('foo');
p.then(() => { throw new Error('noooooo') });
p.catch(() => console.error('caught'));
不同之处在于 fetch().then().catch()
将捕获
由 fetch
promise 或then
promise ;而上述两种方法仅将 failCallback
应用于 fetch
promise。
关于Javascript fetch, catch 永远运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49509408/