我试图理解下面的代码/语法使用await Promise.all; 如何从端点响应中分配数组?
const getState = async (code) => {
try {
const [
{data: dataResponse},
{data: stateDistrictWiseResponse},
{data: statesDailyResponse},
{data: stateTestResponse},
{data: sourcesResponse},
{data: zonesResponse},
] = await Promise.all([
axios.get('https://example.org/data.json'),
axios.get('https://example.org/state_district_wise.json'),
axios.get('https://example.org/states_daily.json'),
axios.get('https://example.org/state_test_data.json'),
axios.get('https://example.org/sources_list.json'),
axios.get('https://example.org/zones.json'),
]);
console.log(stateTestResponse.states_tested_data);
}
}
Promise.all
converts an array of promises into an array of results.axios.get
returns a promise that, when resolved, returns an object with the shape{ data: <result goes here>
您的代码正在破坏返回数组中的那些对象。