參考: https://blog.csdn.net/weixin_44976833/article/details/106210269
// 獲取房間號 let roomalls = [] for (let i = 0; i < res.data.room_device_relations.length; i++) { roomalls.push(getrom(res.data.room_device_relations[i].room_id)) // 返回的是 [Promise] 數組 } console.log(roomalls) Promise.all(roomalls).then(res => { console.log(res) setRoomalls(res) })
// 可以用 async await 變同步的方法
const getrom = async (val) => {
const msg = await roomsthing(val)
console.log(msg)
return msg.data.room_no
}
業務場景 在 一個請求回來的數據中有個數組 多項 的id 要用這些 id 在 去依次請求 接口 就需要先在循環中 用數組 保存 每一個 promers 然后 在用 promise.all 在 把結果 列出來