我们应该选择异步等待Javascript中的Promise [英] should we choose async await over Promise in Javascript
问题描述
我知道 async等待
是镇上的新承诺
,这是一种新的写作方式异步代码,我也知道
I know that the async await
is the new Promise
in the town and it is a new way to write asynchronous code and I also know that
我们没有写 .then
,创建一个匿名函数处理响应
We didn’t have to write .then
, create an anonymous function to handle the response
Async / await
使得最终可以使用相同的构造处理同步和异步错误,好旧的尝试/捕获
Async/await
makes it finally possible to handle both synchronous and asynchronous errors with the same construct, good old try/catch
从 promise
链返回的错误堆栈不能解释错误发生的位置。但是,异步/等待的错误堆栈指向包含错误的函数
The error stack returned from a promise
chain gives no clue of where the error happened. However, the error stack from async/await points to the function that contains the error
AND SO ON ...
AND SO ON...
但
在这里我做了一个简单的基准测试 https://repl.it/repls/FormalAbandonedChimpanzee
在基准测试中,我运行了2次循环100万次。
在第一个循环中,我调用的函数在另一个函数中返回1
我调用的函数是抛出1作为异常。
In the benchmark I have run 2 loops for 1 million times. In first loop I am calling a function that is returning 1 in another function I am calling a function that is throwing 1 as an exception.
调用返回1的函数的第一个循环所花费的时间几乎是将1抛出为错误的函数的一半。
the time taken by first loop which is calling a function that is returning 1 is almost half of the function that is throwing 1 as error.
其中显示了所花费的时间 throw
差不多是返回时间的两倍
Which shows that time taken by throw
is almost double of the time taken by return
node v7.4 linux/amd64
return takes 1.233seconds
1000000
throw takes 2.128seconds
1000000
以下基准代码
function f1() {
return 1;
}
function f2() {
throw 1;
}
function parseHrtimeToSeconds(hrtime) {
var seconds = (hrtime[0] + (hrtime[1] / 1e9)).toFixed(3);
return seconds;
}
var sum = 0;
var start = 0;
var i = 0;
start = process.hrtime();
for (i = 0; i < 1e6; i++) {
try {
sum += f1();
} catch (e) {
sum += e;
}
}
var seconds = parseHrtimeToSeconds(process.hrtime(start));
console.log('return takes ' + seconds + 'seconds');
console.log(sum);
sum = 0;
start = process.hrtime();
for (i = 0; i < 1e6; i++) {
try {
sum += f2();
} catch (e) {
sum += e;
}
}
seconds = parseHrtimeToSeconds(process.hrtime(start));
console.log('throw takes ' + seconds + 'seconds');
console.log(sum);
推荐答案
您的基准测试与<之间的性能无关code> async / await vs raw promises。我只能看到抛出一个错误需要更长的时间来计算。这是预期的。
Your benchmark has nothing to do with the performance between async/await
vs raw promises. All I can see is that throwing an error takes a longer time to compute. This is expected.
回到主要问题,应该使用 async / await
而不是 .then
使用原始承诺?
Back to the main question, should use async/await
rather than .then
with raw promises?
请记住 async / await
是仅仅是原始承诺的语法糖,所以不应该对整体表现产生太大影响。但是,它确实使您的代码更加线性,从而消除了开发人员的大量认知开销。
Keep in mind that async/await
is merely syntactic sugar over raw promises, so there shouldn't be much impact on the overall performance. However, it does make your code more linear which removes a lot of cognitive overhead from the developer.
结论是使用您喜欢的。 Promise可以被polyfill'd但新的语法不能,所以你可能想在决定使用哪种样式时记住这一点。
The conclusion is use what you prefer. Promises can be polyfill'd but new syntaxes cannot, so you might want to keep that in mind when deciding which style to use.
一些误解:
从promise链返回的错误堆栈没有提供错误发生位置的线索
The error stack returned from a promise chain gives no clue of where the error happened
这不是真的。快速检查:
That is not true. A quick check with:
function error() {
return new Promise(function(res, rej) {
res(undefined()); // uh oh
});
}
error().then(console.log, e => console.log("Uh oh!", e.stack));
显示整个错误堆栈,包括位置。
shows the entire error stack including the location.
这篇关于我们应该选择异步等待Javascript中的Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!