How to chain async functions in typescript?

Asked

Viewed 347 times

0

I have two asynchronous functions A and B.

How do I initialize function B only after function A is complete?

import { Component } from '@angular/core';

import { Options } from 'fullcalendar';
import { DataService } from './services/data.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  data: Data[] = [];

  calendarOptions: Options;

  constructor(private dataService: DataService){}

  ngOnInit() {
    const var1 = await this.A();
    const var2 = await this.B();
  }


  async A(){
      this.dataService.getItems().subscribe(items => {
        items.forEach(item => {
          this.data.push(item);
        })
      });

  }

  async B(){
      this.calendarOptions = {
        editable: true,
        eventLimit: false,
        header: {
          left: 'prev,next today',
          center: 'title',
          right: 'month,agendaWeek,agendaDay,listMonth'
        },
        defaultDate: '2017-11-23',
        navLinks: true,
        events: [{
            id:1,
            title:this.data[0].title,
            start:"2017-11-23 09:00:00",
            end:"2017-11-23 11:00:00",
            color:"#0071c5"
        }]
      };

  }
}
  • async / await were made to be used in set with Promises, thing your code doesn’t do. It wouldn’t be better to simply call the B method inside the onNext of subscribe?

  • Dude could you give me an example of how this works? I look for tutorials and find nothing to clarify me.

1 answer

0

The way it’s yours async and awayt do nothing and think they are even wrong. Also there is no reason to save the return of functions since they do not return anything

You can pass a callback to run by the function A after completing its asynchronous operation:

ngOnInit() {
  this.A(this.B);
}
A(callback){
  this.dataService.getItems().subscribe(items => {
    this.data = this.data.concat(items);
    callback();
  });
}
  • I tried to do what you told me but keeps returning error. 'cannot read Property 'data' of Undefined'. As if date was not being loaded and I tested the console load. It’s like B was loading before A.

  • So your problem is different and has no relationship with async await, no need for this loop, can concatenate using this.data = this.data.concat(items), this will concatenate the arrays

  • Guy went bad but still doesn’t work. Same mistake.

  • Are you sure you’re writing the this correctly? Because this error is saying that you are trying to access the property data in a variable with value undefined, i.e., the this it can’t be undefined, must have some spelling error or this error is on some other line that I’m not seeing

  • I have a face. I made sure to copy exactly what you posted here. But you were such fine people that I shared the code with you.https://github.com/Rosnaldo/Calendar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.