I'm wondering what's the best method to loop over an array and use async/await on the elements as you perform a task with them. Being new to the language, I haven't found a definitive answer or I didn't understand the solution (using Promise.all with and map method ?)
This is the code. renderCountry is NOT an async function, but the function where this condition happens is an async. Is it correct how I've done it? If not, how should I implement it? I just wanna make sure the code is non-blocking as I loop over the array's elements,
if (neighboursArr) {
neighboursArr.forEach(async (el, i) => {
await wait(i * 300);
const neighbourRes = await fetch(
`https://restcountries.com/v3.1/alpha/${el}`
);
const neighbourData = await neighbourRes.json();
renderCountry(neighbourData[0], "neighbour");
});
} else {
renderMessage("It is lonely island :)");
}