本文介绍了发送同步请求角度 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想在 angular 6 中发送同步请求这个嵌套的 for 循环.所有的 for 循环都必须等待对方的响应.请在 https://stackblitz.com

i want to send synchrouns request this nested for loop in angular 6. it all for loop must wait each other response. Please give some example in 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
                    })
                  }
                }
              }
            }
          }
        }
      }
}

推荐答案

你需要的是一个

concatMap 不会订阅下一个 observable,直到上一个完成,

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 的更多信息此处.

More info on concatMap here.

这是一个有效的stackblitz

你原来的plateInfo"函数会进行1000多次api调用,希望你知道你在做什么.

Your original "plateInfo" function will make more than 1000 api calls, I hope you know what you are doing.

无论如何,我不得不限制数组中的项目数量以保持 stackblitz 站点的响应.

Anyway, I had to limit the number of items in the array to keep stackblitz site responsive.

这篇关于发送同步请求角度 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 15:29