如果你在一个承诺中有一个承诺,你需要两个 catch 吗?
If you have a promise inside a promise, do you need two catch?
例如,使用 fetch()
进行两次调用(.then() 中的第二个调用),您需要两个 .catch()
还是只需要外部一个就足够了?
我已经尝试复制,但到目前为止还不能。
b = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Solving...');
resolve('solved');
}, 3000);
})
.then(() => {
new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Rejecting...');
reject('error');
}, 1000);
})
.then(() => null)
.catch((error) => {
console.log('Catching error inside.');
console.log(error);
});
})
.catch((error) => {
console.log('Catching error outside.');
console.log(error);
});
这有效,但如果我删除内部 catch()
,我会得到 Unhandled promise rejection
错误
Solving...
Rejecting...
(node:73650) UnhandledPromiseRejectionWarning: error
(node:73650) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:73650) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
No need to multiple catch(). if any error detected then goes to catch()
您需要 return then
块中的承诺才能 catch
它们在一条链中:
b = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Solving...');
resolve('solved');
}, 3000);
})
.then(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Rejecting...');
reject('error');
}, 1000);
});
})
.then(() => null) // You can get rid of this if you don't have anything to do here
.catch((error) => {
console.log('Catching error outside.');
console.log(error);
});
这应该也是首选方式。在代码示例中使用嵌套链接会使代码很难阅读。
例如,使用 fetch()
进行两次调用(.then() 中的第二个调用),您需要两个 .catch()
还是只需要外部一个就足够了?
我已经尝试复制,但到目前为止还不能。
b = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Solving...');
resolve('solved');
}, 3000);
})
.then(() => {
new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Rejecting...');
reject('error');
}, 1000);
})
.then(() => null)
.catch((error) => {
console.log('Catching error inside.');
console.log(error);
});
})
.catch((error) => {
console.log('Catching error outside.');
console.log(error);
});
这有效,但如果我删除内部 catch()
,我会得到 Unhandled promise rejection
错误
Solving...
Rejecting...
(node:73650) UnhandledPromiseRejectionWarning: error
(node:73650) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:73650) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
No need to multiple catch(). if any error detected then goes to catch()
您需要 return then
块中的承诺才能 catch
它们在一条链中:
b = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Solving...');
resolve('solved');
}, 3000);
})
.then(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Rejecting...');
reject('error');
}, 1000);
});
})
.then(() => null) // You can get rid of this if you don't have anything to do here
.catch((error) => {
console.log('Catching error outside.');
console.log(error);
});
这应该也是首选方式。在代码示例中使用嵌套链接会使代码很难阅读。