发送同步请求角度6 [英] send synchrouns request angular 6
问题描述
我想发送syncuns请求,将其嵌套在角度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直到下一个可观察者之前都未订阅 完成
concatMap does not subscribe to the next observable until the previous completes,
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!