【问题标题】:How to wait for the response before running the rest of the code?如何在运行其余代码之前等待响应?
【发布时间】:2019-06-14 08:53:12
【问题描述】:

我想运行一个 axios 请求调用后端的循环,并在重定向页面之前等待所有响应。

在下面的代码中,一旦我收到 200 OK 的响应,我想将它推送到 promiseArray。如果我收到所有 promiseArray 项目,我想将页面重定向到另一个 url。

就我而言,代码似乎并没有真正停下来等待响应。它循环 axios 请求 3 次,但不是等待响应,而是直接运行重定向部分。

有什么想法吗?

 function test(){
  var response = undefined;
  var length = 3;
  var promiseArray = [];

  for(var a=0;a<length;a++){

        var link = 'https://'+hostname+'/';
        var apiUrl = 'api/xxx';
        var token = "123";

        axios.create({
            baseURL: link,
            timeout: 60000,
            headers: {
            Authorization: token
            }
        }).post(apiUrl, {
            ...
        }).then(res => {
            console.log(res);
            promiseArray.push(res);
        }).catch(err=>{
            console.log("err");
            console.log(err);
        });
  }


  response = await axios.all(promiseArray);
  if(response!=undefined){
      window.location.replace("https://"+hostname+"/abc");
  }
}

【问题讨论】:

标签: javascript axios


【解决方案1】:

那是因为promiseArray 是空的,您将结果推送给它。将实际的 Promise 推送到数组中。

async function test(){
  var response = undefined;
  var length = 3;
  var promiseArray = [];

  for(var a=0;a<length;a++){

        var link = 'https://'+hostname+'/';
        var apiUrl = 'api/xxx';
        var token = "123";

        promiseArray.push(
            axios.create({
                baseURL: link,
                timeout: 60000,
                headers: {
                Authorization: token
                }
            }).post(apiUrl, {
                ...
            })
        )
  }


  response = await axios.all(promiseArray);
  if(response!=undefined){
      window.location.replace("https://"+hostname+"/abc");
  }
}

【讨论】:

  • 还需要使test()异步
  • 感谢您提供此代码 sn-p - 这对我来说真的很有效
【解决方案2】:

您希望在重定向页面之前等待所有响应,因此您需要使用 Promise.all()

以下来自 MDN 的示例

var promise1 = Promise.resolve(3);
var promise2 = 42;
var promise3 = new Promise(function(resolve, reject) {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then(function(values) {
  console.log(values);
});
// expected output: Array [3, 42, "foo"]

【讨论】:

    【解决方案3】:

    如果您已经在一个地方使用async / await,为什么不在各处使用它:

     async function test(){
      var length = 3;
    
      for(var a=0; a<length; a++){
    
            var link = 'https://'+hostname+'/';
            var apiUrl = 'api/xxx';
            var token = "123";
    
            let res = await axios.create({
                baseURL: link,
                timeout: 60000,
                headers: {
                    Authorization: token
                }
            }).post(apiUrl, {
                ...
            });
            /* Do whatever you need with res */
      }
      window.location.replace("https://"+hostname+"/abc");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 2017-10-20
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多