Javascript 如何捕获内部异常?
我希望能够捕获在承诺内部抛出的异常,从该承诺的功能之外抛出。请参阅以下代码:Javascript 如何捕获内部异常?,javascript,exception,promise,ecmascript-6,Javascript,Exception,Promise,Ecmascript 6,我希望能够捕获在承诺内部抛出的异常,从该承诺的功能之外抛出。请参阅以下代码: throwError = () => { throw new Error("myError"); }; let iWantToCatchTheErrorThatThisOneThrows = () => { return (new Promise( (resolve) => { resolve() }).then(() => { (n
throwError = () => {
throw new Error("myError");
};
let iWantToCatchTheErrorThatThisOneThrows = () => {
return (new Promise( (resolve) => {
resolve()
}).then(() => {
(new Promise( (resolve, reject) => {
return throwError();
}));
return new Promise((resolve) => setTimeout(resolve, 1000));
})).catch((error) => {
//Never reaches here
console.log(error)
});
};
iWantToCatchTheErrorThatThisOneThrows().then(() => {
console.log("Quit gracefully");
}).catch((error) => {
//Never reaches here
console.log(error);
});
我可以捕捉到内部错误,但我不能通过在中间添加一个捕捉来将其扔到我想要的地方(最终):
let iWantToCatchTheErrorThatThisOneThrows = () => {
return (new Promise( (resolve) => {
resolve()
}).then(() => {
(new Promise( (resolve, reject) => {
return throwError();
})).catch((error) => {
//Prints, but thrown error isn't bubbled anywhere
console.log(error);
throw new Error(error);
});
return new Promise((resolve) => setTimeout(resolve, 1000));
})).catch((error) => {
//Never reaches here
console.log(error)
});
};
iWantToCatchTheErrorThatThisOneThrows().then(() => {
console.log("Quit gracefully");
}).catch((error) => {
//Never reaches here
console.log(error);
});
所有测试都在Nodejs 7.1.0中运行,请按以下方式编辑代码:-
throwError = () => {
throw new Error("myError");
};
let iWantToCatchTheErrorThatThisOneThrows = () => {
return new Promise( (resolve) => {
resolve()
}).then(() => {
//since you are resolving promise , this will be called
return Promise.reject('err')
// returning rejection so we can catch later
}).catch((error) => {
//It will not be called unless some error is thrown
console.log('err',error)
});
};
iWantToCatchTheErrorThatThisOneThrows().then(() => {
console.log("Quit gracefully");
}).catch((error) => {
// Since a rejection is returned by function , it is called
console.log(error);
});
或者,如果要抛出错误:-
throwError = () => {
throw new Error("myError");
};
let iWantToCatchTheErrorThatThisOneThrows = () => {
return new Promise((resolve) => {
resolve()
}).then(() => {
return throwError();
}).catch((error) => {
//Reaches
console.log('err',error)
//Rejection returned to catch later
return Promise.reject('rejection from catch')
});
};
iWantToCatchTheErrorThatThisOneThrows().then(() => {
console.log("Quit gracefully");
}).catch((error) => {
//rejection caught
console.log(error);
});
我意识到我的问题是不合理的,因为我想拒绝已经解决的承诺。问题是,我想立即履行承诺,而不是等待它解决/抛出。一个更清楚的例子如下:
writeToDatabase = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (hasPermissions) {
resolve()
} else {
reject();
}
}, 1000)
});
};
let iWantToCatchTheErrorThatThisOneThrows = () => {
return new Promise((resolve) => {
resolve()
}).then(() => {
//Don't want to wait for this to complete
//but want to catch the error in later catch clauses
//Unfortunately, this is unreasonable since I can't reject
//a promise that might already have been resolved.
//Therefore, refactor to have the same method to handle error
writeToDatabase().catch(handleError);
return Promise.resolve();
}).catch((error) => {
//Will not catch the database error, since it's already resolved
console.log('err', error)
});
};
iWantToCatchTheErrorThatThisOneThrows().then(() => {
console.log("Quit gracefully");
}).catch((error) => {
// Will not catch the database error, since it's already resolved
// Instead, we can reuse the same error handling function
handleError(error);
});
在内部回调中,您进行各种抛出和重新抛出,然后返回一个无关的、已解决的承诺。毫不奇怪,外部的
.catch()
没有什么可捕获的。它不完全适用于我的用例,因为这需要我等待抛出错误的承诺