Reputation: 115
I have a following code (a part of function):
async addMarkers() {
const timeout = ms => new Promise(resolve => setTimeout(resolve, ms));
function geoRequest(order) {
}
function setMarker(order) {
}
for (let i = 0; i < orders.length; i++) {
if(orders[i].google_coords === '') {
await timeout(1000);
geoRequest(orders[i]);
} else {
setMarker(orders[i]);
}
}
}
I need to execute some code only after for
loop is completed. What should I do? Wrap addMarkers
into Promise or something else? I tried to execute callback as parameter of addMarkers
but it didn't help.
Upvotes: 3
Views: 3650
Reputation: 943561
async
functions return promises.
addMarkers().then(() => doSomething());
Upvotes: 3