异步/等待vs那么哪个是最佳性能? [英] Async / await vs then which is the best for performance?

查看:44
本文介绍了异步/等待vs那么哪个是最佳性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中有一个简单的代码,可以简单地在API中执行请求并返回响应.但是在这种情况下,我将有成千上万的请求.因此,哪个代码选项将表现更好,以及为什么.另外,最近推荐哪一种作为好习惯?

I have a simple code in JavaScript that execute a request in an API and return the response, simple. But in this case I will have thousands of requests. So, which one of the code options will perform better, and why. Also which one is recommended as good pratices these days?

第一个选项使用.then来解决承诺,第二个选择使用async/await.

First options is using the .then to resolve the promises and the seccond one is using async / await.

在我的测试中,这两个选项的结果非常相似,没有显着差异,但是我不确定范围.

In my tests the two options had very similar results without significant differences, but I'm not sure in scale.

// Using then
doSomething(payload) {
  const url = 'https://link-here/consultas';
  return this.axios.get(url, {
    params: {
      token: payload.token,
      chave: payload.chave,
    },
   }).then(resp => resp.data);
}

// Using Async / await
async doSomething(payload) {
   const url = 'https://link-here/consultas';
   const resp = await this.axios.get(url, {
   params: {
     token: payload.token,
     chave: payload.chave,
    },
 });
 return resp.data;
}

任何解释都是非常有价值的.

Any explanation will be of great value.

推荐答案

await只是.then()的内部版本(基本上做同样的事情).选择一个而不是另一个的原因实际上与性能无关,而与所需的编码样式或编码便利性有关.当然,解释器还具有在内部使用await进行优化的更多机会,但这不太可能那应该是您决定使用哪个的方式.如果其他所有条件都相同,则出于上述原因,我将选择await.但是,我首先选择使代码更易于编写,理解,维护和测试的

await is just an internal version of .then() (doing basically the same thing). The reason to choose one over the other doesn't really have to do with performance, but has to do with desired coding style or coding convenience. Certainly, the interpreter has a few more opportunities to optimize things internally with await, but its unlikely that should be how you decide which to use. If all else was equal, I would choose await for the reason cited above. But, I'd first choose which made the code simpler to write and understand and maintain and test.

如果使用得当,await通常可以为您节省很多代码您的代码更易于阅读,测试和维护.这就是它被发明的原因.

Used properly, await can often save you a bunch of lines of code making your code simpler to read, test and maintain. That's why it was invented.

两个版本的代码之间没有有意义的区别.当axios调用成功或发生错误时,两者均会达到相同的结果.

There's no meaningful difference between the two versions of your code. Both achieve the same result when the axios call is successful or has an error.

await可以带来更多便利的地方是,如果您有多个需要序列化的连续异步调用.然后,与其将每个括号括在.then()处理程序中以正确链接它们,不如直接使用await并使用更简单的代码.

Where await could make more of a convenience difference is if you had multiple successive asynchronous calls that needed to be serialized. Then, rather than bracketing them each inside a .then() handler to chain them properly, you could just use await and have simpler looking code.

await.then()的常见错误是忘记正确的错误处理.如果您希望此函数中的错误处理只是返回被拒绝的承诺,则两个版本都将执行相同的操作.但是,如果您连续有多个异步调用,并且想要做的事情比返回第一个拒绝还要复杂,那么

A common mistake with both await and .then() is to forget proper error handling. If your error handling desire in this function is to just return the rejected promise, then both of your versions do that identically. But, if you have multiple async calls in a row and you want to do anything more complex than just returning the first rejection, then the error handling techniques for await and .then()/.catch() are quite different and which seems simpler will depend upon the situation.

这篇关于异步/等待vs那么哪个是最佳性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆