mercredi 24 mai 2017

how to reject promise inside a then

I want to be able to reject the entire promise chain if anyone of the promise fails. I want to "catch" this rejection and send an error notification. I have the following code:

let reportMetaData = api.ajaxGet(api.buildV3EnterpriseUrl('reports' + '/' + params.report_id))
                        .catch(error => {
                          if (error.status === constants.HTTP_STATUS.GATEWAY_TIMEOUT) {
                            this.notify.error(this.translate('reports.report_timedout'), this.translate('reports.report_timedout_desc'));
                          } else {
                            this.send('error', error);
                          }
                        });

let aggregateData = reportMetaData.then(success => {
                                                      try {
                                                        return api.xmlRequest('GET', success.aggregationUrls.elements[0].url);
                                                      } catch(error) {
                                                        return Promise.reject();
                                                      }
                                                    }).then(rawData => {
                                                      try {
                                                        return JSON.parse('{' + rawData + '}');
                                                      } catch (error) {
                                                        return Promise.reject();
                                                      }
                                                    }, error => Promise.reject(error));

let aggregateReport = aggregateData.then(data => {
                                                    if (!data || !data.report) {
                                                      return Promise.reject();
                                                    }
                                                    return data.report;
                                                 }, error => {error => { this.notify.error(this.translate('reports.report_timedout'), this.translate('enterprise.reports.malformed_report'))}});

I want the simplest way to reject the entire promise to fail if anyone promise fails. How do I do that from inside the then function?




Aucun commentaire:

Enregistrer un commentaire