I want to make several async request in a loop, but I dont know how to do for pause the current loop until the current request is finished. I tried with a await but it doesnt work, the loop still continues the normal flow, and dispatch the request as soon enters into the loop.
Here is how I make the request:
const pauseLoop = function (){
let _poke_api = " https://pokeapi.co/api/v2/pokemon/{pokemon_name}"
let _pokemons = [
'pikachu',
'arceus',
'dialga',
'fasdf',
'electivire',
'magmortar',
'swampert'
]
function makeRequest( pokemon_name ) {
return new Promise( ( resolve, reject ) =>{
let url = _poke_api.replace('{pokemon_name}', pokemon_name );
fetch( url )
.then( response => {
return response.json();
})
.then( data => {
resolve( data )
})
.catch( error => {
reject('No existe ese pokemon');
});
})
}
function boot(){
_pokemons.forEach( async pokemon_name => {
await makeRequest( pokemon_name )
.then( response => {
// console.log( response )
})
.catch( error => {
console.error( error )
});
});
}
return {
boot : boot
}
}()
pauseLoop.boot();