我有以下代碼:-
for (let i = 0; i < fdlist.length; i ) {
let request = fetch(`${domain}/segment?mode=${mode}&type=${type}`, { method: 'POST', body: fdlist[i] }).then((res) => res.json());
requestlist.push(request)
console.log(requestlist)
}
const allData = Promise.all(requestlist);
allData.then(async (res) => {
它發送多個獲取請求并將它們中的每一個推送到一個串列中,然后我執行 Promise.all 來等待所有請求都解決并立即處理所有結果。
我的問題是,我怎樣才能處理立即解決的承諾,而不是等待所有承諾解決才開始處理?
uj5u.com熱心網友回復:
只需遍歷承諾并添加“then”子句:
for (const p of requestlist) {
p.then(result => ...)
}
// you can always still wait for them all if you want
Promise.all(requestlist).then(...)
您可以then在獲取時添加:
for (...) {
let request = fetch(...)
const processed = request.then(response => ...)
requestlist.push(processed)
}
// wait for it all here if you still want
Promise.all(requestlist).then(...)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/490126.html
標籤:javascript 异步 异步等待 承诺 获取 API
