2 回答

TA貢獻1865條經驗 獲得超7個贊
聽起來你想要這樣的東西
const some = axios.get("/get_some_data").then(res => {
do_some_operation()
return res
})
const other = axios.get("/get_other_data")
Promise.all([some, other]).then(([ someRes, otherRes ]) => {
do_other_operation()
})
這將并行調用兩個 URL。
當第一個解析時,它會調用do_some_operation(). 這個(大概)同步操作成為some承諾解決方案的一部分。other一旦 HTTP 請求完成,承諾就會解決。
一旦some和otherpromises 都解決了,調用do_other_operation()

TA貢獻1797條經驗 獲得超4個贊
使用promise all
Promise.all([
? get_request("/get_some_data"),
? get_request("/get_other_data")
]).then( function(responses) {
? console.log(responses);
? // do what you want
? do_some_operation();
? do_other_operation();
}).catch(function(error) {?
? console.error(error.message);
});
或者
Promise.all([
? get_request("/get_some_data").then(function (resp) {
? ? do_some_operation();
? ? return resp;
? },
? get_request("/get_other_data")
]).then( function(responses) {
? console.log(responses);
? // do what you want
? do_other_operation();
}).catch(function(error) {?
? console.error(error.message);
});
添加回答
舉報