第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用axios進(jìn)行for循環(huán),我怎么知道是否所有數(shù)據(jù)都已加載

使用axios進(jìn)行for循環(huán),我怎么知道是否所有數(shù)據(jù)都已加載

this.allMyFacilities = response[1].data          for (let i = 0; i < this.allMyFacilities.length; i++) {            axios              .get(facilityUsed(this.allMyFacilities[i].id))              .then((response) => {                this.facilitiesOnSupplyChain[this.allMyFacilities[i].id] = response.data              })              .catch((err) => {                // An error will also be thrown if you use cancel.                console.error(err)              })          }我有這樣的代碼。我不知道我有多少設(shè)施。那就是我循環(huán)它,但每個(gè)設(shè)施都有自己的 Axios。我想知道如何知道循環(huán)中的所有數(shù)據(jù)都已完全加載
查看完整描述

2 回答

?
守候你守候我

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以為此使用Promise.all


this.allMyFacilities = response[1].data

const promises = this.allMyFacilities.map((myFacility) => {

    return axios

        .get(facilityUsed(myFacility.id))

        .then((response) => {

            this.facilitiesOnSupplyChain[myFacility.id] = response.data

            return response.data;

        }).catch((err) => {

            // An error will also be thrown if you use cancel.

            console.error(err)

        });

});

Promise.all(promises).then((allResponsesArray) => /* do sth */)


查看完整回答
反對(duì) 回復(fù) 2022-10-21
?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊

假設(shè)所有異步調(diào)用都可以獨(dú)立進(jìn)行,您可以利用該P(yáng)romise.all功能來(lái)實(shí)現(xiàn)這一點(diǎn)。


this.allMyFacilities = response[1].data

await Promise.all(this.allMyFacilities.map(myFacility) => {

  return axios.get(facilityUsed(myFacility.id))

    .then(data => this.facilitiesOnSupplyChain[myFacility.id] =  data);

});

這將允許您axios并行執(zhí)行所有調(diào)用,從而減少整個(gè)過(guò)程的總體執(zhí)行時(shí)間。


PS:我已經(jīng)寫(xiě)了一個(gè)await聲明,假設(shè)你將把這個(gè)功能包裝在一個(gè)async函數(shù)中。


查看完整回答
反對(duì) 回復(fù) 2022-10-21
  • 2 回答
  • 0 關(guān)注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)