Promise is all not access to reject function

I have one service that receives data and I called it 5 times with different parameters to get different data. I called the function to execute on success: it works fine. but if one of the 5 calls fails, I need to do something else that won't happen: it always enters the success function. I am using ionic 4 angular 2 this is a service I have:

 public getdataLookUps(type, lcid): Promise<string[]> {
return new Promise((resolve, reject) => {
  if (this.data[type + lcid]) {
    resolve(this.data[type + lcid]);
    return;
  }
  this.authService.getToken().then(
    (accessToken) => {
      let headers = new Headers({'Authorization': 'bearer ' + accessToken});
      let url = 'error url to test failure case';
      this.http.get(url, {headers: headers})
        .map(res => res.json())
        .toPromise()
        .then(
          (res) => {
            this.data[type + lcid] = res;
            resolve(res);
          },
          (error) => {
            reject(error);
          }
        );
    }
  );
});

      

}

then I wrap a function that calls the service like this: (repeated 5 times with different parameters):

public getAreas() {
    return this.lookupsService.getdataLookUps('Region', this.lcid).then(
      (res) => {
        this.areas = res;
      },
      () => {
        //todo
return Promise.reject('rejection error');
      }
    );
  }

      

then I call 5 functions:

  ngOnInit() {
    this.getCaseCategories();
    this.getAreas();
    this.getWeather();
    this.getMifonProjects();
    this.getUserInfo();
}

      

and I promise .all () here:

ngAfterViewInit(){
 Promise.all(
      [
        this.getCaseCategories(),
        this.getAreas(),
        this.getWeather(),
        this.getMifonProjects(),
        this.getUserInfo(),
      ]
    ).then(
      () => {
        this.loadMap();
      },
      () => {
        this.showErrorMessage = true;
      }
    );
}

      

+3


source to share


2 answers


This code has two callbacks for then

, a success handler and an error handler. If the code is specified as you showed, the error handler returns a success result, so yours Promise.all()

will always succeed:

public getAreas() {
    return this.lookupsService.getdataLookUps('Region', this.lcid).then(
      (res) => {
        this.areas = res;
      },
      () => {
        //todo
      }
    );
  }

      

Don't add an error handler if you really can't handle the error. Instead, just pass the error to the following handler:

public getAreas() {
    return this.lookupsService.getdataLookUps('Region', this.lcid)
    .then(res => this.areas = res);
  }

      



Now yours Promise.all

will give you an error on data retrieval failure.

Also stop nesting your promise handlers:

public getdataLookUps(type, lcid): Promise<string[]> {
    if (this.data[type + lcid]) return Promise.resolve(this.data[type + lcid]);
    return this.authService.getToken().then(
      (accessToken) => {
        let headers = new Headers({'Authorization': 'bearer ' + accessToken});
        let url = 'error url to test failure case';
        return this.http.get(url, {headers: headers})
          .map(res => res.json())
          .toPromise();
     })
     .then((res) => this.data[type + lcid] = res);
}

      

Once you Promise

just return Promise

, there is no need to create a new promise. And if your promise, the success handler, creates another promise to avoid nesting. Your error handler did nothing but propagate the error, so when you don't have a nested promise you don't need that either, just let the error propagate naturally.

+3


source


I solved this by removing the function calls in ngOnInit (); and keep everything the same as my example above (don't change anything in the getDataLookUps service)



0


source







All Articles