简体   繁体   English

如何等待多个异步调用?

[英]How can i wait for multiple async calls?

I have multiply async calls.我有多个异步调用。 For example four of them例如其中四个

this.lazyLoadData();
this.lazyLoadData();
this.lazyLoadData();
this.lazyLoadData();

the problem is that every http request can take different time.问题是每个 http 请求可能需要不同的时间。 In every request i am sending different query parameters to fetch paginated data on backend.在每个请求中,我发送不同的查询参数来获取后端的分页数据。

So in this case the first this.lazyLoadData can finish sometimes later then the second one - depending on the paginated results on the backend.所以在这种情况下,第一个this.lazyLoadData有时可以在第二个之后完成 - 取决于后端的分页结果。

So to prevent that behaviour i tried to use async and await所以为了防止这种行为,我尝试使用async and await

await this.lazyLoadData();
await this.lazyLoadData();
await this.lazyLoadData();
await this.lazyLoadData();

async lazyLoadData(cb?) {
  const filtersParam: any = {
        page: this.filterService.dashboardPage,
        size: 25,
      }

      let response = await this.processMonitorService.monitoring(filtersParam);
      response.then(data => {
         console.log('maked http call');
      });
      ...
}

but the problem is that still, even i use async and await - this four http calls does not happen in order.但问题是,即使我使用 async 和 await - 这四个 http 调用不会按顺序发生。

So in duration of one second i am calling four times lazyLoadData , i await every result there but responses does not come in order.因此,在一秒钟内,我调用了四次lazyLoadData ,我等待那里的每个结果,但响应没有按顺序出现。 So sometimes again third gets executed before the second one etc...所以有时第三个会在第二个之前执行,等等......

How can i solve this ?我该如何解决这个问题?

Why do you need them to be in order?为什么你需要它们有序? Can't you have the required params in advance and then just use the results in order.您不能提前获得所需的参数,然后按顺序使用结果。

Of course, you can make them wait, but it would hurt the overall performance:当然,您可以让他们等待,但这会损害整体性能:

this.lazyLoadData().then(() => this.lazyLoadData())

and etc.等等。

What would be better however is if you don't count on the state, but rather have the parameters come externally.然而,如果您不依赖状态,而是让参数来自外部,那就更好了。 Eg:例如:

await this.lazyLoadData(1);
await this.lazyLoadData(2);
await this.lazyLoadData(3);
await this.lazyLoadData(4);

Where the param is the page number.其中参数是页码。

By the way, what is the use case in general to make 4 requests?顺便说一下,一般来说,发出 4 个请求的用例是什么? Can't you just request a page size of 100 if you need all 4 pages at the same time?如果您同时需要所有 4 个页面,您不能只请求 100 个页面大小吗?

I think that the best way to do this is using Promise.all()我认为最好的方法是使用Promise.all()

your code will look something like this:您的代码将如下所示:

    const arrayOfPromises = [
        this.lazyLoadData(1),
        this.lazyLoadData(2),
        this.lazyLoadData(3),
        this.lazyLoadData(4),
    ]

    Promise.all(arrayOfPromises).then((result) => {
        console.log(result) // this will contain the result of the 4 requests in order
    })

also, there is a bit of refactoring that you need to make in your code.此外,您需要在代码中进行一些重构。

You lazyLoadData function becomes the following你的 lazyLoadData 函数变成了下面的

    async lazyLoadData(cb ? ) {
        const filtersParam: any = {
            page: this.filterService.dashboardPage,
            size: 25,
        }

        return this.processMonitorService.monitoring(filtersParam);
    }

You have to put your code in an async function:你必须把你的代码放在一个异步函数中:

 (async()=>{ await this.lazyLoadData(); await this.lazyLoadData(); await this.lazyLoadData(); await this.lazyLoadData(); }());

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

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