[英]Asynchronous exception handling with bluebird promises
What is the best way to handle this scenario.处理这种情况的最佳方法是什么。 I am in a controlled environment and I don't want to crash.我处于受控环境中,我不想崩溃。
var Promise = require('bluebird');
function getPromise(){
return new Promise(function(done, reject){
setTimeout(function(){
throw new Error("AJAJAJA");
}, 500);
});
}
var p = getPromise();
p.then(function(){
console.log("Yay");
}).error(function(e){
console.log("Rejected",e);
}).catch(Error, function(e){
console.log("Error",e);
}).catch(function(e){
console.log("Unknown", e);
});
When throwing from within the setTimeout we will always get:当从 setTimeout 内抛出时,我们总是会得到:
$ node bluebird.js
c:\blp\rplus\bbcode\scratchboard\bluebird.js:6
throw new Error("AJAJAJA");
^
Error: AJAJAJA
at null._onTimeout (c:\blp\rplus\bbcode\scratchboard\bluebird.js:6:23)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
If the throw occurs before the setTimeout then bluebirds catch will pick it up:如果抛出发生在 setTimeout 之前,那么 bluebirds catch 会捡起它:
var Promise = require('bluebird');
function getPromise(){
return new Promise(function(done, reject){
throw new Error("Oh no!");
setTimeout(function(){
console.log("hihihihi")
}, 500);
});
}
var p = getPromise();
p.then(function(){
console.log("Yay");
}).error(function(e){
console.log("Rejected",e);
}).catch(Error, function(e){
console.log("Error",e);
}).catch(function(e){
console.log("Unknown", e);
});
Results in:结果是:
$ node bluebird.js
Error [Error: Oh no!]
Which is great - but how would one handle a rogue async callback of this nature in node or the browser.这很好 - 但是如何在节点或浏览器中处理这种性质的流氓异步回调。
Promises are not domains , they will not catch exceptions from asynchronous callbacks. Promise 不是领域,它们不会从异步回调中捕获异常。 You just can't do that.你就是不能那样做。
Promises do however catch exceptions that are thrown from within a then
/ catch
/ Promise
constructor callback.然而,Promise 确实会捕获从then
/ catch
/ Promise
构造函数回调中抛出的异常。 So use所以使用
function getPromise(){
return new Promise(function(done, reject){
setTimeout(done, 500);
}).then(function() {
console.log("hihihihi");
throw new Error("Oh no!");
});
}
(or just Promise.delay
) to get the desired behaviour. (或只是Promise.delay
)以获得所需的行为。 Never throw in custom (non-promise) async callbacks, always reject the surrounding promise.永远不要抛出自定义(非承诺)异步回调,总是拒绝周围的承诺。 Use try-catch
if it really needs to be.如果确实需要,请使用try-catch
。
After dealing with the same scenario and needs you are describing, i've discovered zone.js , an amazing javascript library , used in multiple frameworks (Angular is one of them), that allows us to handle those scenarios in a very elegant way.在处理了您所描述的相同场景和需求后,我发现了zone.js ,一个了不起的 javascript 库,用于多个框架(Angular 就是其中之一),它允许我们以非常优雅的方式处理这些场景。
A Zone is an execution context that persists across async tasks.区域是跨异步任务持续存在的执行上下文。 You can think of it as thread-local storage for JavaScript VMs您可以将其视为 JavaScript VM 的线程本地存储
Using your example code :使用您的示例代码:
import 'zone.js'
function getPromise(){
return new Promise(function(done, reject){
setTimeout(function(){
throw new Error("AJAJAJA");
}, 500);
});
}
Zone.current
.fork({
name: 'your-zone-name',
onHandleError: function(parent, current, target, error) {
// handle the error
console.log(error.message) // --> 'AJAJAJA'
// and return false to prevent it to be re-thrown
return false
}
})
.runGuarded(async () => {
await getPromise()
})
Thank @Bergi.谢谢@Bergi。 Now i know promise does not catch error in async callback.现在我知道 promise 不会在异步回调中捕获错误。 Here is my 3 examples i have tested.这是我测试过的 3 个示例。
Note: After call reject, function will continue running.注意:呼叫拒绝后,功能将继续运行。
Example 1: reject, then throw error in promise constructor callback示例 1:reject,然后在 promise 构造函数回调中抛出错误
Example 2: reject, then throw error in setTimeout async callback示例 2:拒绝,然后在 setTimeout 异步回调中抛出错误
Example 3: reject, then return in setTimeout async callback to avoid crashing示例 3:拒绝,然后在 setTimeout 异步回调中返回以避免崩溃
// Caught
// only error 1 is sent
// error 2 is reached but not send reject again
new Promise((resolve, reject) => {
reject("error 1"); // Send reject
console.log("Continue"); // Print
throw new Error("error 2"); // Nothing happen
})
.then(() => {})
.catch(err => {
console.log("Error", err);
});
// Uncaught
// error due to throw new Error() in setTimeout async callback
// solution: return after reject
new Promise((resolve, reject) => {
setTimeout(() => {
reject("error 1"); // Send reject
console.log("Continue"); // Print
throw new Error("error 2"); // Did run and cause Uncaught error
}, 0);
})
.then(data => {})
.catch(err => {
console.log("Error", err);
});
// Caught
// Only error 1 is sent
// error 2 cannot be reached but can cause potential uncaught error if err = null
new Promise((resolve, reject) => {
setTimeout(() => {
const err = "error 1";
if (err) {
reject(err); // Send reject
console.log("Continue"); // Did print
return;
}
throw new Error("error 2"); // Potential Uncaught error if err = null
}, 0);
})
.then(data => {})
.catch(err => {
console.log("Error", err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.