Javascript 如何在承诺中不改变我的数组顺序

Javascript 如何在承诺中不改变我的数组顺序,javascript,promise,here-api,Javascript,Promise,Here Api,我目前正在使用hereapi制作一个路由系统。我从文本中的一系列方向开始。像这样 directions: [ "2 Rue de l'Euron, 54320 Maxéville", "34 Rue Sainte-Catherine, 54000 Nancy", //aquarium "401 Avenue de Boufflers, 54520 Laxou", //grand frais "42 Rue Kléber, 540

我目前正在使用hereapi制作一个路由系统。我从文本中的一系列方向开始。像这样

directions: [ 
        "2 Rue de l'Euron, 54320 Maxéville",
        "34 Rue Sainte-Catherine, 54000 Nancy", //aquarium
        "401 Avenue de Boufflers, 54520 Laxou", //grand frais 
        "42 Rue Kléber, 54000 Nancy", //regalo pizza
        "33/45 Avenue de Metz, 54320 Maxéville", //lidl
        "2 Rue de l'Euron, 54320 Maxéville"
    ]
我将这些指示发送到here API,以获得这些指示的lat和lng。问题是我使用承诺,有时这些方向会以不同的顺序返回,但我至少需要第一个和最后一个方向保持不变

我已经读过了。我已经尝试了wait,但是我不能,因为它是一个异步函数。没有承诺;但它说需要回拨

这是我通过地理编码器获得坐标的方法

getCoordinates(query) {
                return new Promise((resolve, reject) => {
                    this.geocoder.geocode({ searchText: query }, data => {

                        //Si il y'a une response
                        if(data.Response.View[0].Result.length > 0) {
                            data = data.Response.View[0].Result.map(location => {
                                return {
                                    address: query,
                                    lat: location.Location.DisplayPosition.Latitude + "", //.toString() marche pas 
                                    lng: location.Location.DisplayPosition.Longitude + ""
                                };

                            });
                            resolve(data);
                        }
                        //Si non
                        else {
                            reject({ "message": "No data found" });
                        }
                    }, error => {
                        reject(error);
                    });
                });
            },
在这里,我试着把它们装上

load(directions){
     directions.map(direction => 
         this.getCoordinates(direction).then(response => 
              console.log(response[0]))
                )

            }
有时我会有这样的反应。未订购

{address: "2 Rue de l'Euron, 54320 Maxéville", lat: "48.70283", lng: "6.13316"}
{address: "401 Avenue de Boufflers, 54520 Laxou", lat: "48.69347", lng: "6.13732"}
{address: "33/45 Avenue de Metz, 54320 Maxéville", lat: "48.70848", lng: "6.16666"}
{address: "2 Rue de l'Euron, 54320 Maxéville", lat: "48.70283", lng: "6.13316"}
{address: "34 Rue Sainte-Catherine, 54000 Nancy", lat: "48.69507", lng: "6.18847"}
{address: "42 Rue Kléber, 54000 Nancy", lat: "48.68373", lng: "6.16838"}

仅在调用
Promise后记录响应。每个方向的映射承诺上的所有

load(directions){
  Promise.all(
    directions.map(direction => this.getCoordinates(direction))
  )
  .then((coordinates) => {
    coordinates.forEach((coordinate) => {
      console.log(coordinate);
    });
  });
}

Promise.all
将一个Promise数组作为参数,并按照与原始数组相同的顺序解析为每个Promise resolve值的数组,这正是您想要的。

太好了,它可以工作。但我想知道这是怎么保证的,一切都有效吗?那之后的.getCoordinates和之后的Promise.all有什么区别呢。顺便说一句,你能不能把我的问题向上投票,让你也向上投票。
Promise.all
将一个Promise数组作为参数,并按照与原始数组相同的顺序解析为每个Promise resolve值的数组-这正是你想要的。例如
Promise.all([Promise.resolve(2),Promise.resolve(3)])
解析为
[2,3]