您将如何解决此“ Promise <any>”不匹配签名'(value:any):{} | PromiseLike <{}>”错误

时间:2019-06-18 14:28:47

标签: angular promise

我正在尝试按照用户承诺进行同步执行。

我遵循了github论坛中列出的说明,没有运气。

getData2(user: string, guid: string) {
  return new Promise<any>((resolve, reject) => {
    //doing some stuff in here
    resolve();
  });
}

getData1(user: string, guid: string) {
  return new Promise<any>((resolve, reject) => {
    //doing some other stuff in here
    resolve();
  });
}

userCheck(user: string) {
  //checking some array in here
}

userMethod(someArray: any){
  var i: any;
  var length = someArray.length;
  for (i=0; i < length; i++){
    this.getData1(someArray[i], userID).then(
      this.getData2(someArray[i], userID)).then(
          this.userCheck(someArray[i]));
      });
    })}
  }

这个想法是,我想先完成getData1,然后再进行getData2,然后再进行userCheck

当我按照上面的布局进行操作时,会收到错误消息:

Argument of type 'void' is not assignable to parameter of type '(value: {}) => {} | PromiseLike<{}>'.

解决此问题后,我发现userCheck函数在getData2完成之前仍在执行。我不知道为什么。 getData2函数如下所示。

getCCData(user: string, id: string){
    console.log('grabbing CC information', id, user);
        return new Promise((resolve, reject) =>
        {
        this._apiService.getAssertion(id).toPromise().then(assert =>
{
    return this._apiService.getToken(assert.toString(), user);
}).then((data: any) =>
{   
    return this._apiService.ccMeta(data.access_token.toString(), id);
}).then(parseString(Promise)).then((information: any) =>
{
    this.ccData.push(
    {
        key: 'userStatus',
        value: information.entry
    });
});

    resolve();
        });
}

我不确定这是否是设计此功能的正确方法。任何额外的见解都会很棒。

1 个答案:

答案 0 :(得分:1)

该错误表明then()期望函数作为参数,同时向其提供另一个then()调用的返回值,即Promise。

要解决该错误,请将then()调用内的调用更改为箭头函数:

for (const item of someArray) {
  this.getData1(item, userID).then(() =>
    this.getData2(item, userID).then(() =>
      this.userCheck(item)
    )
  );
}
相关问题