【问题标题】:promise.all not access in rejection functionpromise.all 不能在拒绝功能中访问
【发布时间】:2017-06-12 15:11:13
【问题描述】:

我有一个获取数据的服务,我用不同的参数调用了 5 次以获取不同的数据。 我调用了一个函数在成功的情况下执行:它工作正常。 但是如果 5 个调用中的一个失败,我需要做一些其他没有发生的事情:它总是进入成功功能。 我正在使用离子 4 角度 2 这是我的服务:

 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);
          }
        );
    }
  );
});

}

然后我像这样包装调用服务的函数:(使用不同的参数重复 5 次):

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

然后我调用 5 个函数:

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

我在这里做 promise.all() :

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

【问题讨论】:

  • 如果我在 getArea() 包装函数中添加 return Promise.reject ,我得到这个错误:Uncaught (in promise): 1
  • 或许这篇文章有用:stackoverflow.com/questions/30362733/…
  • 避免Promise constructor antipattern 中的getdataLookUps!
  • @Bergi 我没听懂你
  • 好吧,如果浏览器没有缓存任何内容,那么设置正确的Cache-Control 响应标头(并且-如果您打开了开发人员工具-取消选中覆盖网络选项卡中的“始终从服务器重新加载”复选框常规缓存处理)。

标签: javascript angular ionic-framework promise ionic2


【解决方案1】:

此代码有两个针对then 的回调、一个成功处理程序和一个错误处理程序。如果代码如您所见,错误处理程序将返回成功结果,因此您的 Promise.all() 将始终成功:

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

除非您真的能够在此处处理错误,否则不要添加错误处理程序。而是让错误传播到下一个处理程序:

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

现在你的Promise.all会在数据查找失败时给你一个错误。

同时停止嵌套你的 Promise 处理程序:

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);
}

一旦你有了Promise,只需返回Promise,就不需要创建新的Promise。如果您的承诺成功处理程序创建另一个承诺返回以避免嵌套。您的错误处理程序只会传播错误,因此当您没有嵌套的 Promise 时,您也不需要它,只需让错误自然传播即可。

【讨论】:

  • 同样的错误:未捕获(在承诺中):响应状态为:0 对于 URL:null
【解决方案2】:

我通过删除 ngOnInit(); 中的函数调用解决了这个问题。 并保持与我上面的示例相同的所有内容(不更改 getDataLookUps 服务中的任何内容)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 2013-09-27
    • 2020-02-29
    相关资源
    最近更新 更多