javascript - 一个又一个的承诺 一个又一个的承诺
问题描述
所以我想从我的数据库(通过 axios)中获取一些东西,如下所示[按顺序]:
- 问题
- 选择
- 答案
我可以用这个得到它:
// Get Questions
axios.get(url_here)
.then(res => {
if (res.status === 200) {
// Save Questions...
// Get Choices
axios.get(url_here)
.then(res => {
if (res.status === 200) {
// Save Choices...
// Get Answers
axios.get(url_here)
.then(res => {
if (res.status === 200) {
// Save Answers
}
})
}
})
}
})
但从表面上看,这段代码注定会以某种方式失败。
如果我做这样的事情:
// Get Questions
axios.get(url_here)
.then(res => {
if (res.status === 200) {
// Save Questions...
}
})
// Get Choices
axios.get(url_here)
.then(res => {
if (res.status === 200) {
// Save Choices...
}
})
// Get Answers
axios.get(url_here)
.then(res => {
if (res.status === 200) {
// Save Answers
}
})
Get Choices
Get Questions
不管成功与否都可以调用。如果我的请求成功,我想Get Choices
“等待和倾听”怎么办?Get Questions
非常感谢!
解决方案
推荐阅读
- ada - Gnatchop 未解析文件
- angular - toString 的 ionic 4 最喜欢的问题并保存数据
- django - 使用 Django Rest Framework 计算对象
- python - 通过键列表在嵌套字典中附加(而不是替换)项目
- networking - 基于带有 PF_RING 的端口对入站流量进行优先级排序并将其路由到虚拟接口
- spring-batch - 在 spring 批处理应用程序上实现 graalvm 是否可以提高其性能
- node.js - 在腻子会话超时后,Angular 会话被终止
- sharepoint - 无法将共享点文件夹同步到本地设备
- javascript - Electron-forge 使我的 Windows 安装文件运行应用程序但不安装它
- java - JavaConfig:将spring xml替换为java配置