如何在承诺中不改变我的数组顺序
How to not alter my array order in a promise
我目前正在使用此处-api 制作路由系统。我从文本中的一系列说明开始。像这样。
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"
]
我将这些方向发送到此处 API 以获得这些方向的纬度和经度。问题是我使用 promises,有时这些指示 return 的顺序不同,但我至少需要第一个和最后一个留在同一个地方。
我已经阅读了 and 。我已经尝试过 await 但我不能,因为它是一个异步函数。没有承诺;但它说它需要回调。
这就是我使用这里的地理编码器获取坐标的方式
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.all
后记录响应:
load(directions){
Promise.all(
directions.map(direction => this.getCoordinates(direction))
)
.then((coordinates) => {
coordinates.forEach((coordinate) => {
console.log(coordinate);
});
});
}
Promise.all
将 Promise 数组作为参数,并解析为每个 Promise 解析值的数组,与原始数组 的顺序相同 -正是你想要的。
我目前正在使用此处-api 制作路由系统。我从文本中的一系列说明开始。像这样。
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"
]
我将这些方向发送到此处 API 以获得这些方向的纬度和经度。问题是我使用 promises,有时这些指示 return 的顺序不同,但我至少需要第一个和最后一个留在同一个地方。
我已经阅读了
这就是我使用这里的地理编码器获取坐标的方式
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.all
后记录响应:
load(directions){
Promise.all(
directions.map(direction => this.getCoordinates(direction))
)
.then((coordinates) => {
coordinates.forEach((coordinate) => {
console.log(coordinate);
});
});
}
Promise.all
将 Promise 数组作为参数,并解析为每个 Promise 解析值的数组,与原始数组 的顺序相同 -正是你想要的。