以角度保留 http 调用的顺序 [英] Preserve the order of the http calls in angular

查看:22
本文介绍了以角度保留 http 调用的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我在 SO 没有成功.

I'm trying to solve the order problem I'm facing with several approaches that I found here on SO without a success.

我有一个方法,我正在为 leaflet 层的数组加载一些数据:

I have a method, where I'm loading some data for an array of leaflet layers:

private loadSelectedTileLayersCapabilities(): void {

        let tempTileLayer;

        this.selectedTileLayerIds.forEach(
            (selectedTileLayer: string) => {
                tempTileLayer = this.getTileLayerById(selectedTileLayer);

                this.capabilitiesService.getTileLayerDimensions(tempTileLayer.url, tempTileLayer.name, tempTileLayer.id)
                .subscribe(
                    dimensions => this.displayNewTileLayer(dimensions)
                );
            }
        );
    }

然后我有一个方法,其中发生了 http 调用:

And then I have a method, where the http call is happening:

public getTileLayerDimensions(urlToFormat: string, tileLayerName: string, tileLayerId: string): Observable<Dimensions> {

        const capabilitiesUrl = `serviceUrl`;

        return this.httpClient.get(capabilitiesUrl, {responseType: "text"})
            .map(res => {

                // Doing stuff with data

                return dataForLayer;
            });

    }

问题是,displayNewTileLayer(dimensions) 方法是以随机顺序调用的.有没有办法保留物品的存储顺序selectedTileLayerIds 数组?

The problem is, the displayNewTileLayer(dimensions) method is called in random order. Is there a way to preserve the order in which the items were stored in selectedTileLayerIds array?

推荐答案

由于 http 调用是异步的,响应可能不会以请求发出的相同顺序到达.您可以做的是创建一个请求列表,创建一个 forkJoin 和等待所有响应解决.然后,您可以为所有响应调用 displayNewTileLayer(dimensions) 方法.

Since the http-calls are asynchronous, the responses may not arrive in the same order the requests were made. What you could do is to create a list of requests, create a forkJoin and wait for all responses to resolve. You can then call the displayNewTileLayer(dimensions)-method for all responses.

这是一个例子

    const httpCalls = []; // store the requests here
    for (let i = 0; i < 3; i++) {
      httpCalls.push(this.http.get('someUrl').map(response => response));
    }
    forkJoin(httpCalls).subscribe(res => {
      // all responses completed. returns an array of data (one for each response).
      console.log('res', res);
    });

在您的情况下,此代码可能有效:(代码未经测试,您可能需要在代码中导入 forkJoin 运算符)

In you case, this code may work: (code not tested, and you may have to import the forkJoin operator in your code)

import { forkJoin } from 'rxjs/observable/forkJoin';

private loadSelectedTileLayersCapabilities(): void {

    let tempTileLayer;
    let requests = []:

    this.selectedTileLayerIds.forEach(
        (selectedTileLayer: string) => {
            tempTileLayer = this.getTileLayerById(selectedTileLayer);
            const request = this.capabilitiesService.getTileLayerDimensions(tempTileLayer.url, tempTileLayer.name, tempTileLayer.id)
            requests.push(request);

        }
    );
    forkJoin(requests).subscribe(res => {
      res.forEach(dimension => this.displayNewTileLayer(dimension));
    })
}

这篇关于以角度保留 http 调用的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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