Promise all multiple gets from Storage inside a forEach loop


#1

Hi. re-writed question to see if i have more luck with answers. Im stuck atm.

i have a forEach loop that reads a lot of records from Storage:

// load each record from Storage
this.idsAbiertasAll.forEach( (value, key, index) => {
    this.storage.get('incidencia-'+value).then((data)=> {
         let dataParsed = JSON.parse(data);
         // update arrays
         this.incidencias.push(dataParsed); 
         console.log('loaded  '+dataParsed.id);
    });
});

Question: how can i check that forEach has fully loaded all records from storage before continuing with code? help pls, im quite newbie with promises and async coding


#2

Push all the this.storage.gets in an array which you resolve through Promise.all(array-with-all-gets)

Which you can check through a then

One of many solutions


#3

solved. thanks =))))))


#4

Something like this?

let blabla=[];
this.idsAbiertasAll.forEach( (value, key, index) => {

  this.blabla.push(
    this.storage.get('incidencia-'+value).then((data)=> {
         let dataParsed = JSON.parse(data);
         // update arrays
         this.incidencias.push(dataParsed); 
         console.log('loaded  '+dataParsed.id);
    }))
});

Promise.all(blabla).then(_=>{console.log('happy coder')}).catch(err=>{console.log('SHOOT, one failed, aborting')})