简体   繁体   English

JS:Promise.all 有什么不同

[英]JS: what's the different in Promise.all

I just wonder if there is different between those 2 calls by await Promise.all([/*... /*]) In my performance test, it took the same time.我只是想知道await Promise.all([/*... /*])在我的性能测试中,这两个调用之间是否存在不同。

This is my method to be invoked by the Promise.all:这是我要被 Promise.all 调用的方法:

const makeFetch1 = async () => {
  axios.get("https://jsonplaceholder.typicode.com/todos/1");
};

const makeFetch2 = async () => {
  return axios.get("https://jsonplaceholder.typicode.com/todos/1");
};

1) await Promise.all([makeFetch1(), makeFetch1(), ...])
2) await Promise.all([makeFetch2(), makeFetch2(), ...])

Both have the same execution time result, what i should choose?两者都有相同的执行时间结果,我应该选择什么?

The first one will fire of a request and not return it .第一个将触发请求而不返回它 so makeFetch1() will result in a Promise that resolves immediately.所以makeFetch1()将导致立即解决的 Promise 。 This is almost certainly never what you want.这几乎肯定不是你想要的。 An async function should await or return inside somewhere, otherwise it may as well be a standard function (that returns nothing).异步 function 应该在某处awaitreturn ,否则它也可能是标准的 function(什么都不返回)。

Even if you don't care about what the request resolves to, you should still use the result of the request (by returning or await ing, just so you can see if the request succeeds or not - and communicate to the user that there was a problem if it fails)即使您不关心请求解析为什么,您仍然应该使用请求的结果(通过返回或await ,以便您可以查看请求是否成功 - 并与用户沟通有如果失败则有问题)

The second one will fire of a request and return it .第二个将触发一个请求并返回它 so makeFetch1() will result in a Promise that resolves once the request resolves - which is likely to take a number of milliseconds.所以makeFetch1()将产生一个 Promise ,一旦请求解决,它就会解决 - 这可能需要几毫秒。

There are several major differences and multiple coding mistakes.有几个主要差异和多个编码错误。

First off this code:首先关闭这段代码:

const makeFetch1 = async () => {
  axios.get("https://jsonplaceholder.typicode.com/todos/1");
};

is an async function that calls axios.get() and then immediately returns a promise that is already resolved and has an undefined resolved value.是一个async function 调用axios.get() ,然后立即返回一个 promise 已经解析并具有undefined的解析值。 Nothing here waits for the axios.get() call to finish.这里没有等待axios.get()调用完成。

Your second code block:您的第二个代码块:

const makeFetch2 = async () => {
  return axios.get("https://jsonplaceholder.typicode.com/todos/1");
};

calls axios.get() and returns the promise that it creates.调用axios.get()并返回它创建的 promise。 So, the promise this function returns will be tied to when the axios.get() call completes and the resolved value will be the resolved value of the axios call. So, the promise this function returns will be tied to when the axios.get() call completes and the resolved value will be the resolved value of the axios call.

Differences:差异:

  1. You could use the makeFetch2() function to get the result of the axios.get() call (resolved value or reject error).您可以使用makeFetch2() function 来获取axios.get()调用的结果(解析值或拒绝错误)。 You could not use makeFetch1() to get the result of the axios.get() call because that result is not communicated back to the caller in any way.您不能使用makeFetch1()来获取axios.get()调用的结果,因为该结果不会以任何方式传回给调用者。

  2. Similarly, makeFetch2() will allow the caller to know when the axios operation has completed.同样, makeFetch2()将允许调用者知道 axios 操作何时完成。 makeFetch1() will not. makeFetch1()不会。

  3. If the axios.get() operation rejected, then makeFetch1() would result in an unhandled rejection error because nothing is listening for the rejection.如果axios.get()操作被拒绝,那么makeFetch1()将导致未处理的拒绝错误,因为没有任何东西在监听拒绝。

Then, these two:然后,这两个:

await Promise.all([makeFetch1, makeFetch1, ...])
await Promise.all([makeFetch2, makeFetch2, ...])

Are both pretty useless because you're not calling any of the functions so nothing is executing.两者都非常无用,因为您没有调用任何函数,因此没有执行任何操作。 These will both just resolve immediately with a resolved value of an array of function references.这些都将立即使用 function 引用数组的解析值解析。 None of the axios calls are executed because nothing actually calls makeFetch1() or makeFetch2() .没有执行任何 axios 调用,因为实际上没有调用makeFetch1()makeFetch2()


If you meant to actually execute the functions with these two:如果您打算使用这两个实际执行功能:

await Promise.all([makeFetch1(), makeFetch1(), ...])
await Promise.all([makeFetch2(), makeFetch2(), ...])

Then, the first one just resolves immediately with an array of undefined values as the resolved value because remember the makeFetch1() return value isn't connected in any way to the axios.get() call.然后,第一个立即使用undefined值数组作为已解析值解析,因为请记住makeFetch1()返回值与axios.get()调用没有任何关联。

The second one will properly wait for all the axios.get() calls in makeFetch2() to complete and will resolve with an array of values from the axios.get() calls.第二个将正确等待 makeFetch2() 中的所有makeFetch2() axios.get()调用完成,并将使用来自axios.get()调用的一组值进行解析。


Both have the same execution time result, what i should choose?两者都有相同的执行时间结果,我应该选择什么?

That's because as you've implemented them, you aren't calling the makeFetchN() functions in either case.这是因为当您实现它们时,在任何一种情况下都不会调用makeFetchN()函数。 So, they aren't doing anything and thus have the same execution time.因此,它们什么都不做,因此具有相同的执行时间。

If you change the implementation to actually call the functions as in:如果您将实现更改为实际调用函数,如下所示:

await Promise.all([makeFetch1(), makeFetch1(), ...])
await Promise.all([makeFetch2(), makeFetch2(), ...])

There will be a significant timing difference because the one based on makeFetch1() does not wait for any of the axios.get() calls to finish and does not communicate back results or completion and is subject to uncaught rejections.将存在显着的时序差异,因为基于makeFetch1()的方法不会等待任何axios.get()调用完成并且不会返回结果或完成,并且会受到未捕获的拒绝。

The one based on makeFetch1() has no practical use as you could just do:基于makeFetch1()的那个没有实际用途,你可以这样做:

makeFetch1();
makeFetch1();
...

with the exact same result because makeFetch1() just returns an immediately resolved promise so passing them all to Promise.all() isn't doing anything useful.结果完全相同,因为makeFetch1()只返回一个立即解析的 promise 所以将它们全部传递给Promise.all()并没有做任何有用的事情。

makeFetch1() is probably just not useful since it doesn't inform the caller of completion, error or resolved value. makeFetch1()可能只是没有用,因为它不会通知调用者完成、错误或已解决的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM