我正在尝试捕捉可能在另一个承诺的范围内引发的错误。 (抱歉,这令人困惑...)
async function throwingError () {
throw new Error('this error needs to be caught'); // I don't know if I need to return, throw or something else. This function may or may not have an error
};
async function mainFunction () {
promiseFunction()
.catch(error => {
throwingError();
// I've tried throw throwingError() and return Promise.reject(throwingError());
});
};
mainFunction().catch(error => `caught: ${console.error}`);
现在,它无法捕获错误。 据我所知,这是行不通的,因为承诺会在不同的时间完成,但我不确定。
您需要返回承诺。