我有這樣的代碼時:試圖數據推到我的數組爲保證這不保存數據
let splatshArtData = [];
splatshArt.getSplatchArt(participants[i].championId).then((splatshArtUrl) => {
splatshArtData.push(splatshArtUrl);
});
console.log(splatshArtData);
我要添加「splatshArtUrl」我的陣列,但是這不工作,當我嘗試打印數據,這不會打印什麼,我不知道該怎麼做,任何想法?異步函數getSplatchArt
解決它的承諾之後
let splatshArtData = [];
splatshArt.getSplatchArt(participants[i].championId).then((splatshArtUrl) => {
splatshArtData.push(splatshArtUrl);
console.log(splatshArtData);
});
的功能then
運行裏面,所以它運行的console.log
項目被添加到陣列之前:
'.save'方法是什麼? –
@ Alexandru-IonutMihai哦,沒什麼,髒的代碼:p –
將'console.log(splatshArtData);'移到'.then()'塊中,它會起作用。你的函數調用是異步的,但你認爲它是同步的,因此是錯誤。 –