问题描述
我想了解如何调试基于 promise 的异步代码.Promises 是指基于 ECMAScript 6 的 promises,调试是指使用内置的 chrome 或 firefox 调试器.
I am trying to understand how to debug asynchronous code that is based on promises. By Promises I mean ECMAScript 6 based promises and by debugging I mean using the built-in chrome or firefox debugger.
我遇到的问题是 - 发生错误时,无论我如何拒绝"它,我似乎都无法获得堆栈跟踪.
What I am having trouble with - is that when an error occurs I can't seem to get the stack trace no matter how I 'reject' it.
我试过这些:
console.log(new Error('Error occured'));
throw new Error('Throwing an Error');
return new Error('Error returned by the onRejected function');
reject(new Error('Pass Error to the reject function'));
但这些都没有返回代码中的实际错误或堆栈跟踪.
But none of those returns the actual error in the code, or the stack trace.
所以我的问题是 - 如何正确调试 javascript Promises?
So my question is - how to properly debug javascript Promises?
推荐答案
这是一个值得讨论的好话题,不幸的是,对于原生 Promise 来说,这实际上很难.
This is a great topic to discuss, the sad news are this is actually quite hard with native promises.
在 Chrome 中调试原始 ES6 承诺是可怕的.这是因为它们会默默地抑制错误,并且无论何时你忽略一个捕获,它都不会给你任何承诺失败的迹象. 更新:Chrome 现在记录未处理的拒绝 (请参阅此链接了解操作方法)
Debugging raw ES6 promises in Chrome is horrible. This is because they will silently suppress errors and whenever you omit a catch it will not give you any indication that the promise failed. Update: Chrome now logs unhandled rejections (see this link for how)
Promise.resolve("foo").then(function(){
throw new Error("You will never see this");// silent failure
});
在 Firefox 中,情况要好一些,因为它们执行未处理的拒绝检测 - 但是,它仍然很不稳定,如果您将 promise 分配到任何地方,它都将不起作用.
In Firefox, things are a bit better since they perform unhandled rejection detection - however, it's still flakey and if you assigned the promise anywhere it won't work.
包括 Bluebird - 它是 ES6 承诺的超集,你可以直接在里面交换它,它有一个更丰富的 API,速度更快,并且具有惊人的堆栈跟踪.它在构建时考虑到了调试问题,并包含出色的错误处理功能.
Include Bluebird - it's a superset of ES6 promises and you can swap it right inside, it has a richer API, it's faster and it has amazing stack traces. It's built with debugging in mind and includes great error handling facilities.
加入 Bluebird 后,请致电:
Once you've included Bluebird, call:
Promise.longStackTraces();
这会稍微减慢它的速度(它仍然会非常快)并且会给你惊人的错误信息.例如:
Which will slow it down a bit (it'll still be very fast) and will give you amazing error messages. For example:
Promise.resolve().then(function outer() {
return Promise.resolve().then(function inner() {
return Promise.resolve().then(function evenMoreInner() {
a.b.c.d()
});
});
});
在原生 promises 中 - 这将是一个无声的失败,并且很难调试 - 使用 Bluebird promises 这将在默认情况下在你的控制台中显示一个大的红色错误给你:
In native promises - this will be a silent failure and will be very hard to debug - with Bluebird promises this will show a big red error in your console by default giving you:
ReferenceError: a is not defined
at evenMoreInner (<anonymous>:6:13)
From previous event:
at inner (<anonymous>:5:24)
From previous event:
at outer (<anonymous>:4:20)
From previous event:
at <anonymous>:3:9
at Object.InjectedScript._evaluateOn (<anonymous>:581:39)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:540:52)
at Object.InjectedScript.evaluate (<anonymous>:459:21)
完成调试后 - 您可以将其换出并返回到本机承诺.就我个人而言,我很重视知道我在生产中存在错误,所以我不推荐它,但它肯定是可行的.
Once you're done debugging - you can swap it out and go back to native promises. Personally I value knowing I have errors in production so I don't recommend it but it's certainly doable.
这篇关于如何调试 javascript 承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论