Is there some way to call asynchronous function in NOT for
loop (e.g. foreach, map) using async/await
and continue execution of the rest code only after all asynchronous functions completed?
const layers = ["layer1", "layer2", "layer3"];
async function connect(layer: string): Promise<object> {
return new Promise((resolve, reject) => {
setTimeout(function () {
resolve(layer);
}, 1000);
});
}
(async function () {
for (let i = 0; i < layers.length; i++) {
const result = await connect(layers[i]);
console.log(result, "connected");
}
console.log("All layers are connected");
})();