0

I have the following method that loops through a list of objects and for each node you need to execute a promise. What is the best way to execute two or more asynchronous operations in a row within a for loop?

 async cargarEstadosPools() {
    let that = this;
    let nodo: Nodo;      
 
    for (var i = 0; i < that.aplicacionEntorno.entorno.nodos.length; i++) {
        this.appService.loading = true;
        nodo = that.aplicacionEntorno.entorno.nodos[i];    
        await that.obtenerDatos.Metodo(this.ruta + 'api/nodo' + '/' + aplicacionId)
        .then((res: any) => {             
            if (res != 'Started' && res != 'Stopped') {
                nodo.errorPool = res;
                nodo.estadoPool = 'Error';
            }
            else {
                nodo.errorPool = '';
                nodo.estadoPool = res;
            }
            nodo.ejecutandoAccionNodo = false;
            that.appService.loading = false;
        })
    }     
}
ararb78
  • 1,137
  • 5
  • 19
  • 44

2 Answers2

2

Since you are using angular you may consider using observables. Converting the above to observables would become something like below

import { from, forkJoin } from 'rxjs';
import { tap } from 'rxjs/operators';

...

cargarEstadosPools() {
  this.appService.loading = true;
  return forkJoin(
    aplicacionEntorno.entorno.nodos.map(nodo => 
      from(obtenerDatos.Metodo(this.ruta + 'api/nodo' + '/' + aplicacionId))
    )
  ).pipe(
    tap(() =>  this.appService.loading = false)
  )
}

We are creating an array of observables using the code

    aplicacionEntorno.entorno.nodos.map(nodo => 
      from(obtenerDatos.Metodo(this.ruta + 'api/nodo' + '/' + aplicacionId))
    )

We then join this array with forkJoin([observable1, obsevable2, ...])

We use pipe and tap operator to set loading to false once all the observables have completed

Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
  • where would you evaluate the result of each observable? ... if (res != 'Started' && res != 'Stopped') { nodo.errorPool = res; nodo.estadoPool = 'Error'; } – ararb78 Apr 13 '21 at 09:46
  • You can use `tap` operator or inside subscribe function – Owen Kelvin Apr 13 '21 at 12:45
  • I don't think it could be used, because I don't know how many subscriptions it would have. Could you give an example? – ararb78 Apr 15 '21 at 14:59
1

To fire the execution of async functions parallel for each other within a for loop, you should do something like this:

await Promise.all(that.aplicacionEntorno.entorno.nodos.map(nodo => {  
        await that.obtenerDatos.Metodo(this.ruta + 'api/nodo' + '/' + aplicacionId)
        .then((res: any) => {             
            if (res != 'Started' && res != 'Stopped') {
                nodo.errorPool = res;
                nodo.estadoPool = 'Error';
            }
            else {
                nodo.errorPool = '';
                nodo.estadoPool = res;
            }
            nodo.ejecutandoAccionNodo = false;
            that.appService.loading = false;
        })
    }   

From this answer.

ForestG
  • 17,538
  • 14
  • 52
  • 86