[英]In JS, how do I catch errors thrown by user-defined functions' asynchronous calls?
I'm writing a library that takes user-defined functions. 我正在编写一个使用用户定义函数的库。 I don't have control over what they do, but I want to catch all errors that they cause. 我无法控制它们的工作,但我想抓住它们引起的所有错误。
They might also make asynchronous calls like setTimeout
, which might throw Errors. 他们还可能进行setTimeout
类的异步调用,这可能会引发错误。 I want to catch those errors too. 我也想抓住那些错误。
For example— 例如-
// Suppose this is passed as an argument to the module
var userFunction = function() {
setTimeout(function() { throw Error("fail") }, 200);
}
// This is module code
try {
userFunction();
} catch (e) {
console.log("Caught error");
}
—fails with an error and prints a stack trace. —失败,并显示堆栈跟踪。 The catch
doesn't trigger. catch
不会触发。
I can see how this happens: The error is thrown from the function passed to setTimeout
, which is called after the try
- catch
has passed, in a different context. 我可以看到这种情况是如何发生的:传递给setTimeout
的函数引发了错误,该函数在try
- catch
通过之后在不同的上下文中调用。
How do I work with that? 我该如何处理? Is it even possible? 可能吗
If given a function that might call setTimeout
or other asynchronous processes within it, how can I catch errors they might throw? 如果给定的函数可以调用setTimeout
或其中的其他异步进程,那么如何捕获它们可能引发的错误?
You can use window.onerror
to catch all error 您可以使用window.onerror
捕获所有错误
try { setTimeout(function() { throw Error("fail") }, 2000); } catch (e) { console.log("Caught"); } window.onerror = function (){ document.body.innerHTML = "Test"; }
or you can use try catch
inside async method 或者您可以在异步方法中使用try catch
setTimeout(function() {
try {
throw Error("fail");
} catch (e) {
console.log("Caught");
}
}, 2000);
You can use Promises. 您可以使用Promises。
With jQuery, something like: 使用jQuery,类似:
var dfd = jQuery.Deferred();
setTimeout(function() {
dfd.reject("some error msg");
}, 1000);
$.then(dfd.promise()).then(function() {
//blank, this is success callback
}, function(msg) {
//throw your error
});
Full doc: https://api.jquery.com/deferred.promise/ 全文: https : //api.jquery.com/deferred.promise/
EDIT: can use any Promise implementation. 编辑:可以使用任何Promise实现。 Such as kriswowal/q https://github.com/kriskowal/q 如kriswowal / q https://github.com/kriskowal/q
You don't need to include jQuery. 您不需要包括jQuery。 You can use javascripts built in Promise object: 您可以使用Promise对象中内置的javascript:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/prototype https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise/prototype
you have: 你有:
y = x;
x returns a promise: x返回一个承诺:
function() {
...
return promise;
}
All of the asynchronous calls in x are layered: x中的所有异步调用都是分层的:
x is last async handler: x是最后一个异步处理程序:
x = function () {
...
// a is second to last async success handler etc..
var j = new Promise (a);
j.then(function() {
// a returned resolve
if (some successful condition) {
resolve(what ever arguments you want to pass y);
} else {
reject(what ever arguments you want to pass y);
},
function (e) {
// error returned by a
reject(e);
}
);
return j;
};
then you can do: 那么您可以执行以下操作:
y.then(function() {},
function() {
console.log("Caught");
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.