问题描述
我在 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 来解决 promise,第二个选项是使用 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()
的一个常见错误是忘记了正确的错误处理.如果您在此函数中的错误处理愿望只是返回被拒绝的承诺,那么您的两个版本的做法都是一样的.但是,如果您连续有多个异步调用,并且想要做任何比返回第一个拒绝更复杂的事情,那么 await
和 .then()
/.catch()
的错误处理技术完全不同,看起来更简单的将取决于情况.
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.
这篇关于Async/await vs 那么哪个是最好的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!