如何将多个then()承诺解析为一个



在这段代码中,我使用了多个then()方法,我只想将其转换为一个方法,这是怎么可能的。

getGreeting = () => {
fetch(url)
.then((response) => response.json())
.then((result) => result.data)
.then((data) => printCards(data))
.catch((err) => {
console.log(err);
});
};

注意asyncawait关键字

fetch(url)
.then(async (response) => {
const json = await response.json();
printCards(json.data);
})
.catch((err) => {
console.log(err);
});
};

最新更新