gpt4 book ai didi

javascript - 等待循环先完成

转载 作者:行者123 更新时间:2023-12-02 00:36:29 25 4
gpt4 key购买 nike

我想在进入另一个循环之前先完成所有结果。我怎样才能做到这一点?

function callback_Original(results, status) { 
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker_Original(results[i]);
}
}
}

它有时总是给出很少的地方。

function createMarker_Original(place) {
var photos = place.photos;
if (!photos) {
return;
}

var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});



google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div style="float:left"><img src="' + photos[0].getUrl({'maxWidth': 120, 'maxHeight': 120})
+ '"></div><div style="float:right; padding: 10px;"><b>Name: </b>'+ place.name +'<br/>'+
'<b>Coordinates : </b>'+ place.geometry.location +'<br/>'+
'<b>Type: </b>'+ type +'<br/>');
infowindow.open(map, this);
});
}

最佳答案

使用 Promise 怎么样?(ES6 代码)

function callback_Original(results, status) {
return new Promise((resolve, reject) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker_Original(results[i]);
}
resolve();
}else
reject("Places service error");
});
}

然后就用

callback_Original(a,b)
.then(response => {
// Loop finished, what to do nexT?
})
.catch(error => {
// Error
console.log(error);
});

关于javascript - 等待循环先完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49333699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com