首页 > 解决方案 > 一个又一个的承诺 一个又一个的承诺

问题描述

所以我想从我的数据库(通过 axios)中获取一些东西,如下所示[按顺序]:

  1. 问题
  2. 选择
  3. 答案

我可以用这个得到它:

// 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 ChoicesGet Questions不管成功与否都可以调用。如果我的请求成功,我想Get Choices“等待和倾听”怎么办?Get Questions

非常感谢!

标签: javascriptreactjsaxios

解决方案



推荐阅读