我想等待三个 HTTP 请求完成,然后再调用另一个函数来处理三个 HTTP 请求返回的数据。
我尝试循环房间数(相当于所需的 HTTP 请求数),然后将设备推入数组。在将设备数组传递给下一个要处理的函数之前,我需要完成所有三个 HTTP 请求。
getDeviceListByRoom(rooms_id_array: string[]) {
console.log('The rooms_id_array is: ', rooms_id_array);
this.room_device_map = new Map;
let count = 0;
for (const id of rooms_id_array) {
const devicesByRoom = this.Services.getDeviceByRoom(id);
devicesByRoom.subscribe((res: any) => {
if (res.code === 200) {
// this statement will map the list of devices to the room.
this.room_device_map.set(id, res.data);
// this statement will just push the list of devices from different room into one array.
this.all_devices_list.push(res.data);
console.log('count is: ', count++);
}
}, err => {
console.error('ERROR', err);
});
console.log('In all_devices_list is: ', this.all_devices_list);
}
console.log('Lalalalal');
}
The code above will return 'Lalalala' first followed by the console print out of the count variable. Understandably, the for...of function is non blocking...?
慕的地10843
三国纷争
忽然笑
相关分类