我正在尝试从 API 获取数据,但结果一直未定义。我正在使用 promise.all 链接 API 调用,然后链接 promise 以对数据执行其他操作。我似乎无法将我的结果传递给州。
这是有问题的代码:
if (gameDifficulty === "mixed") {
const result = await Promise.all([
fetchClient.getData(
getEndpoint(
`amount=${countValues[0]}&difficulty=easy&type=${questionType}`
)
),
fetchClient.getData(
getEndpoint(
`amount=${countValues[1]}&difficulty=medium&type=${questionType}`
)
),
fetchClient.getData(
getEndpoint(
`amount=${countValues[2]}&difficulty=hard&type=${questionType}`
)
),
])
.then(function (responses) {
console.log(responses);
console.log(result); //returns undefined
return Promise.all(
responses.map(function (response, idx) {
return fetchClient.processData(response, "results");
})
);
})
.then(function (data) {
console.log(data);
console.log(result); // returns undefined
return [...data[0], ...data[1], ...data[2]];
});
}