发送同步请求角度6

时间:2019-05-15 19:22:00

标签: javascript angular synchronization angular6

我想发送syncuns请求,将其嵌套在角度6中的for循环中。所有for循环必须彼此等待响应。请在https://stackblitz.com

中举例
protected plateInfo(debug = true) {
      for (let i = 0; i < 8; i++) {
        for (let k = 0; k < 8; k++) {
          if (k % 2 !== 0) {
            for (let threshBlock = 21; threshBlock < 31; threshBlock++) {
              if (threshBlock % 2 !== 0) {
                for (let treshWeight = 5; treshWeight < 19; treshWeight++) {
                  if (treshWeight % 2 !== 0) {
                   this.getPLateInfo.getInfoPlate({
                      qausLast: i,
                      qausParam: k,
                      treshBlock: threshBlock,
                      treshWeight: treshWeight
                    }).subscribe(_data => {
                      this.result.push(_data)
                      _data.input1 = i
                      _data.input2 = k
                    })
                  }
                }
              }
            }
          }
        }
      }
}

2 个答案:

答案 0 :(得分:0)

尝试使用等待/异步

async getResult(): MyCustomObject {
    if (typeof this.result === 'undefined') 
    {
        // save result
        this.result = await this.service.call()
        .toPromise()
        .then(resp =>resp as MyCustomObject);//Do you own cast here

    }
    return this.result;
}

答案 1 :(得分:0)

您需要的是

  

concatMap直到下一个可观察者都订阅   完成

from([your source array])
    .pipe(
        concatMap(
            (item in your array) => {
                return this.getPLateInfo.getInfoPlate(....
            }
        )
    )
    .subscribe(
        (received data from your api call) => {
            process received data here...
        }
    );

从以下位置导入它们:

import { from } from 'rxjs';
import { concatMap } from 'rxjs/operators';

有关concatMap here的更多信息。

编辑:

这是工作中的stackblitz

您原来的“ plateInfo”函数将进行1000多次api调用,希望您知道自己在做什么。

无论如何,我必须限制数组中的项目数,以保持stackblitz网站的响应速度。