Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何等待http请求完成?Javascript_Javascript_Http_Ionic Framework_Request - Fatal编程技术网

如何等待http请求完成?Javascript

如何等待http请求完成?Javascript,javascript,http,ionic-framework,request,Javascript,Http,Ionic Framework,Request,我是新的堆栈溢出,所以我希望我能找到人解决我的问题 事情是这样的:我正在用ionic创建一个应用程序,我需要执行一些HTTP请求,因为我需要来自GoogleMaps的数据。在此之前,我只处理很少的航路点,所以我只能执行一个请求,但现在,由于我们的限制,我不得不执行其中的许多请求 问题是,我试图用位置之间的距离填充一个矩阵,但javascript我是异步的,因此它不起作用。。。这是我的一段代码,仅供您理解。我尝试创建一个for循环以填充我的矩阵,我想找到一种方法,强制脚本在继续之前等待每个请求的答

我是新的堆栈溢出,所以我希望我能找到人解决我的问题

事情是这样的:我正在用ionic创建一个应用程序,我需要执行一些HTTP请求,因为我需要来自GoogleMaps的数据。在此之前,我只处理很少的航路点,所以我只能执行一个请求,但现在,由于我们的限制,我不得不执行其中的许多请求

问题是,我试图用位置之间的距离填充一个矩阵,但javascript我是异步的,因此它不起作用。。。这是我的一段代码,仅供您理解。我尝试创建一个for循环以填充我的矩阵,我想找到一种方法,强制脚本在继续之前等待每个请求的答案

for(this.k=0;this.k{
console.log(错误状态);
this.teste=error.error;//错误消息为字符串
console.log(error.headers);
});

}
您需要收集数组中的所有
Promise
对象,然后使用
Promise.all(集合)。然后(()=>{})

你没有在这里发布你的全部代码…所以我在做一些猜测。下面的代码将给你一些关于如何解决问题的想法

var promises = [];
for (this.k = 0; this.k < this.adresses_split.length - 1; this.k ++){
    this.url = this.url1 + this.adresses_split[this.k] + this.url2 + this.phraseCl  + this.url3
    console.log(this.url)

    promises.push(
        this.httpB.get(this.url,{},{}).then(data =>{
            for ( this.i = 1; this.i < 4; this.i++ ){
                for(this.j = this.i + 1 + 3*this.k ; this.j< this.clients.length + 1; this.j++ ){
                    this.distances.push(this.i- 1 + 3*this.k);
                    this.distances.push(this.j - 1);
                    this.distances.push(data.data.split('elements')[this.i].split('duration')[this.j].split('value" : ')[1].split(' ')[0]);
                }
            } 
        }).catch(error => {
              console.log(error.status);
              this.teste=error.error; // error message as string
              console.log(error.headers);
        })
    );

}

Promise.all(promises).then(() => {
    ... do you stuff...
}

注意:我没有测试上面的代码,因此可能会出现一些错误。我希望它能帮助您。

您需要收集数组中的所有
Promise
对象,然后使用
Promise.all(集合)。然后(()=>{})

你没有在这里发布你的全部代码…所以我在做一些猜测。下面的代码将给你一些关于如何解决问题的想法

var promises = [];
for (this.k = 0; this.k < this.adresses_split.length - 1; this.k ++){
    this.url = this.url1 + this.adresses_split[this.k] + this.url2 + this.phraseCl  + this.url3
    console.log(this.url)

    promises.push(
        this.httpB.get(this.url,{},{}).then(data =>{
            for ( this.i = 1; this.i < 4; this.i++ ){
                for(this.j = this.i + 1 + 3*this.k ; this.j< this.clients.length + 1; this.j++ ){
                    this.distances.push(this.i- 1 + 3*this.k);
                    this.distances.push(this.j - 1);
                    this.distances.push(data.data.split('elements')[this.i].split('duration')[this.j].split('value" : ')[1].split(' ')[0]);
                }
            } 
        }).catch(error => {
              console.log(error.status);
              this.teste=error.error; // error message as string
              console.log(error.headers);
        })
    );

}

Promise.all(promises).then(() => {
    ... do you stuff...
}

注意:我没有测试上面的代码,因此可能会出现一些错误。我希望它能对您有所帮助。

您显然在寻找
Promise.all([])。then()
。只有当所有承诺(在本例中是http调用)都完成时,它才会进入
然后
回调。您显然在寻找
Promise.all([]).then()
。只有当所有承诺(在本例中为http调用)都被取消时,它才会进入
然后
回调已完成。感谢您的回答!我必须承认我不掌握承诺,但我将尝试一下,我会告诉您!但谢谢:)出于某种原因,我忽略了他们删除了我回答您的帖子…我说我没有掌握承诺,也没有成功显示HTTP请求通常发送回来的内容…您可以修改您的scri吗请让它显示第一个请求的结果?只是为了让我了解结构。谢谢!你是我的英雄:*谢谢你的回答!我必须承认我不懂承诺,但我会尝试一下,我会告诉你!但是谢谢:)因为一个原因,我忽略了他们删除了我回答你的帖子…我说我没有回答你er承诺并没有成功显示HTTP请求通常返回的内容…请修改脚本以显示第一个请求的结果,好吗?只是为了让我理解结构。谢谢!你是我的英雄:*