Answer the question
In order to leave comments, you need to log in
How to make a promise work in a loop as it should?
There is data in the format:
let data = {
"orders": [
{
"order_id": 1,
"adres": "Москва-сити,первый красногвардейский проезд,16б"
}, {
"order_id": 2,
"adres": "Подольск, мкр Климовск, по-кт 50-летия Октября, 10"
}, {
"order_id": 3,
"adres": "г. Павловский Посад, 4-й Захаровский переулок, д. 6"
}, {
"order_id": 4,
"adres": "волгоградский проспект, д1572"
}
]
};
ymaps.ready(() => {
let myMap = new ymaps.Map("delivery-map", {
center: [55.610906, 37.488429],
zoom: 8
});
let myGroup = new ymaps.GeoObjectArray({}, {
strokeWidth: 14,
geodesic: true
});
for (let i in data.orders) {
let id = data.orders[i].order_id;
let location = data.orders[i].address;
console.log(`${id}\t${location}`);
ymaps.geocode(location, {
results: 1
}).then(r => {
let c = r.geoObjects.get(0).geometry.getCoordinates(),
let p = new ymaps.Placemark(c, {
iconContent: id,
hintContent: location
});
console.log(`${id}\t${location}`);
myGroup.add(p);
});
}
myMap.geoObjects.add(myGroup);
});
p = new ymaps.Placemark(c, { iconContent: id, hintContent: location });
the data of the current iteration was written (id is needed to launch the gallery). Answer the question
In order to leave comments, you need to log in
Instead of var id use let id.
why is this happeningymaps.Placemark works asynchronously.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question