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

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

问题描述

我正在尝试通过几种方法来解决我遇到的订单问题,这些方法在

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天全站免登陆