简体   繁体   English

Angular 4 等待 promise/observable 进入循环

[英]Angular 4 wait for promise/observable into a loop

I can't find a valid solution for following case我找不到以下案例的有效解决方案

I want to create the following loop :我想创建以下循环:

  1. ForEach elemenent (A) of an array ForEach 元素 (A) 数组
  2. Populate a new array (B)填充一个新数组 (B)
  3. If key doesn't exists in array (B) call a firebase service getting data如果数组 (B) 中不存在键,则调用 Firebase 服务获取数据
  4. Verify data and update Statistic验证数据并更新统计信息
  5. at the end of all (A) key, generate statistic of execution and expose...在所有 (A) 键的末尾,生成执行统计信息并公开...

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 )在继续循环之前,我需要等待并链接 Firebase 数据的响应,问题是我需要在主循环中与来自 firebase 的数据进行交互(因此我无法在 subscribe 或 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(); }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM