简体   繁体   English

如何将异步函数推送到数组而不执行未来的 Promise.all

[英]How to push async functions to an array without execution for a future Promise.all

I have some async functions that make calls to a REST API and return the result.我有一些异步函数可以调用 REST API 并返回结果。 I want to use Promise.all to call several of these functions at once and await the full result.我想使用 Promise.all 一次调用其中几个函数并等待完整结果。 I understand how Promise.all works and this isn't a problem with Promise.all.我了解 Promise.all 的工作原理,这不是 Promise.all 的问题。 This is a problem with using Array.push to dynamically create an array of the async functions I want to call.这是使用 Array.push 动态创建我想要调用的异步函数数组的问题。

So here's the scenario.所以这是场景。 A user of my app loads a page.我的应用程序的用户加载了一个页面。 That page needs to retrieve data from an API.该页面需要从 API 检索数据。 It uses those async functions I mentioned to do that.它使用我提到的那些异步函数来做到这一点。 However, the data the page needs to retrieve is different depending on the parameters set.但是,页面需要检索的数据根据​​设置的参数而有所不同。 So sometimes, it may need to retrieve a list of users, a specific customer, and a specific project.所以有时,它可能需要检索用户列表、特定客户和特定项目。 Other times, it may need to get all users and all projects and no customers.其他时候,它可能需要获得所有用户和所有项目,而没有客户。

So my code looks something like this:所以我的代码看起来像这样:

created() {
  let promiseList = []
  if (options.getCustomers) { promiseList.push(listCustomers())
  if (options.getCustomer) { promiseList.push(getCustomer(customerId)) }
  if (options.getUsers) { promiseList.push(getUsers()) }
  await Promise.all(promiseList)
}

So this works to some degree.所以这在某种程度上是有效的。 Promise.all is working just fine. Promise.all 工作正常。 The problem is that I can't push a function to an array without it being called immediately.问题是我不能在没有立即调用函数的情况下将函数推送到数组。 Therefore, each of the functions that I'm pushing to the promiseList array gets called twice: immediately and again with Promise.all.因此,我推送到 promiseList 数组的每个函数都会被调用两次:使用 Promise.all 一次又一次地调用。

How can I push the async function to the array as a reference so that it's not executed immediately?如何将异步函数作为引用推送到数组,以便它不会立即执行?

Put in the array the reference to the function and call then with a map when you use the Promise.all将对该函数的引用放入数组中,然后在使用 Promise.all 时使用映射调用

created() {
  let promiseList = []
  if (options.getCustomers) { promiseList.push({ func: listCustomers })
  if (options.getCustomer) { promiseList.push({ func: getCustomer, arg: customerId}) }
  if (options.getUsers) { promiseList.push({ func: getUsers }) }
  await Promise.all(promiseList.map( (prom) => prom.func(prom.arg) ))
}

You can also push the function也可以推送功能

created() {
     let promiseTasks = []; // meaning name is better
     if (options.getCustomers) {
         promiseTasks.push(() => listCustomers());
     }
     if (options.getCustomer) {
         promiseTasks.push(() => getCustomer(customerId));
     }
     if (options.getUsers) {
         promiseTasks.push(() => getUsers());
     }

     await Promise.all(promiseTasks.map((func) => func()));
 }

Note: I don't think it's the use case you want to use Promise.all, because its looks like all get requests, and it's better to be separated to different endpoints.注意:我不认为是你要使用Promise.all的用例,因为它看起来像所有的get请求,最好分开到不同的端点。

Request option is useally can be options like: limit, sort, projection, populateQuery, preventPopulate.请求选项通常可以是以下选项:限制、排序、投影、populateQuery、preventPopulate。

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

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