0

I don't understand why it doesn't enter in the for loop, any ideas?

async getAllApis() {
    const apis: Api[] = [];

    console.log( this.all_api_id ); /* Output on console (correct):
                                        0: "aura-insight-events"
                                        1: "audit"
                                        2: "consent-events"
                                        3: "backoffice"
                                     */
    
    console.log( typeof(this.all_api_id) ); # Output on console (correct): Object

    for await ( const id of this.all_api_id ) {
      
      console.log( id ); # No answer    ¿?

      this.getAPIbyID( 'apis', id ).subscribe( ( api: Api ) => {

        console.log( api ); # No answer (obviously)

        apis.push( api[0] );
      });
    }

    console.log( apis ); # Output on console:     []    => length: 0

    this.apis = apis;
}

I've tried in several ways but neither of them made any effect.. I don't understand why the code is acting like this -- if anyone does understand, please let me know

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MANZARBEITIA
  • 103
  • 6
  • 1
    Why the `await` on the for loop line? It is an array of strings you are iterating over right? I see no other use of await here. You are also using an observable with subscribe. Subscribe does not work with async/await keywords, async/await is for a `Promise`, not an `Observable`. – Igor Sep 17 '20 at 14:28
  • 1
    See the aforementioned [suggested duplicate](https://stackoverflow.com/q/14220321/1260204). Asynchronous calls are a common and critical building block in writing/designing an application. It is critical that you understand how to work with asynchronous calls in javascript, and by extension typescript. Understanding these core concepts will help you become a better programmer and also ensure you do not keep "stubbing your toe" on the same problem. – Igor Sep 17 '20 at 14:32

0 Answers0