2

I can't find a valid solution for following case

I want to create the following loop :

  1. ForEach elemenent (A) of an array
  2. Populate a new array (B)
  3. If key doesn't exists in array (B) call a firebase service getting data
  4. Verify data and update Statistic
  5. at the end of all (A) key, generate statistic of execution and expose...

I need to wait and chain the response of Firebase data before continue the loop, and the problem is that I need to interact with data from firebase in the main loop ( so I can't implement it in subscribe or then )

This is the Part of code that i want to create :

        // Here i check if user exist in array (B)
        let IDUtente = RifEvento.esisteUtente(Ordini.IDUtente) ;

        // If not exists i'll get data from FIREBASE
        if (IDUtente == -1) {
           let NewUser = new TipoStatUtente() ;
           NewUser.Chiave = Ordini.IDUtente ;
           IDUtente = RifEvento.Utenti.push(NewUser) ;
           IDUtente = IDUtente - 1 ;

           // I NEED to wait the end of this function before continue with loop
           this.statoUtente.getUser(IDUtente).then(dati => {
              console.log('Sottoscritto dati utente') ;
              let user : TipoSingoloUtente = dati ;
              NewUser.sonoscuola = user.sonoscuola ;
              if (!NewUser.sonoscuola) NewUser.Intestazione = user.Cognome + ' ' + user.Nome ; else NewUser.Intestazione = user.Scuola.Nome ;
              if (NewUser.sonoscuola) RifEvento.NumScuole += 1 ; else RifEvento.NumUtenti += 1 ;
           })

        }

        console.log(IDUtente) ;

        // Utente Esiste aggiorno le sue statistiche
        RifEvento.Utenti[IDUtente].Commissioni  += Ordini.costoCommissione ;
        RifEvento.Utenti[IDUtente].Incasso      += Ordini.parziale ;
        if (Ordini.Tipo == 'T') {                  
           RifEvento.Utenti[IDUtente].NumTicket += Ordini.numbiglietti ;
         } else {
           RifEvento.Utenti[IDUtente].NumIscritti  += 1 ;
           RifEvento.Utenti[IDUtente].NumBallerini += Ordini.IDBallerini.length ;
          }



      }

And this is the function :

getUser(IDUtente) : Promise<TipoSingoloUtente> {
return this.db.object('user/' + IDUtente).map(users => {
return users ;})
.first()
.toPromise(); }
Jayani Sumudini
  • 1,409
  • 2
  • 22
  • 29
  • Is the "main loop" a `for` loop? – guest271314 Oct 14 '17 at 10:04
  • Even the loop is synchronized, it cannot block for the asynchronous call. So after you asynchronous call is invoked, the loop will continue. You need to use the callback function to control the final result. (maybe all callbacks are finished then return the final result) – Xin Meng Oct 14 '17 at 10:27
  • ok, the point is that the loop is not finish at the async call... i need the data of the Async call for continue the loop... There is a way for make the function wait? ( like if i've all the data in a local array? ) – Riccardo Cassano Oct 14 '17 at 13:14

1 Answers1

1

Maybe this pseudocode can show the implementation for put the asynchronous call in the loop and make sure all the calls are finished then do something.

global count = 0
loop A[i] i:0->A.size
    var B
    if(A[i] NOT IN B)
        async_function(A[i], callback(result))
    else
        //do something
        count++ 
    end if  
end loop A

callback(result)
    //do something
    count++
    if count == A.size 
        //all data has been processed
        //you also can only count the asyn calls
        //final report
    else
        //do nothing
end callback
Xin Meng
  • 1,982
  • 3
  • 20
  • 32
  • the problem is that i need to continue the "Main" loop with the data of the async call... so with the call back i need to set other "counter" and make decision for continue the main loop... therefore i don't need to call all the time the "async" funtion... if i've the user in the main loop i'll just elaborate the data... – Riccardo Cassano Oct 14 '17 at 13:16
  • I think you need to re-think it, you cannot block the main loop for the async, so you cannot let the main loop wait for you async. So you need to think it bases on the async programming without traditional programming. – Xin Meng Oct 14 '17 at 14:34
  • ok, so there is no way for make the main loop waiting ( chain ) for the answer? – Riccardo Cassano Oct 19 '17 at 10:32
  • yes, you can check this https://stackoverflow.com/a/42173481/2000468 which explain the loop of javascript – Xin Meng Oct 19 '17 at 21:10