js中for循環多次請求,返回數據不按照順序返回的處理


參考: 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 在  把結果 列出來    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM