javascript - 如何将多个 then() 承诺合二为一
问题描述
在这段代码中我使用了多种then()
方法,我只想将它转换为只有一种,这怎么可能。
getGreeting = () => {
fetch(url)
.then((response) => response.json())
.then((result) => result.data)
.then((data) => printCards(data))
.catch((err) => {
console.log(err);
});
};
解决方案
注意async
和await
关键字
fetch(url)
.then(async (response) => {
const json = await response.json();
printCards(json.data);
})
.catch((err) => {
console.log(err);
});
};
推荐阅读
- android - Google Play 游戏:UnlockAchievement 调用时出现 401 错误
- reactjs - Apollo 客户端:缓存更新后组件不呈现(反应变量)
- jquery - 在 jQuery 中为 API 创建 cURL POST 请求?
- php - 您的要求无法解决为作曲家中的一组可安装软件包错误
- html - 猫头鹰轮播有错误的下一个和上一个标签
- mongodb - 如何在猫鼬中的选定字段上执行搜索
- python - 在 Azure Function for Pypandoc 中安装 Pandoc
- java - ImageButton 不起作用 Android Studio Java
- javascript - Next.js - 服务器上的样式化组件样式化中断
- json - 从 Shopify API 获取 line_items 到 Google 表格中