代码之家  ›  专栏  ›  技术社区  ›  xzk

布尔函数中的角度使用承诺

  •  0
  • xzk  · 技术社区  · 7 年前

    在Angular中,我可能无法正确理解Promise()的概念,但我应该如何修改下面的代码,以便checkExistence()可以返回布尔值?

    public checkExistence(value: string): boolean{
      var exist = false;
      return this.getRefData().then((rec: string[]) => {
        return rec.some(el => {
            el === value;
        });
      });
    }
    
    private async getRefData() {
      return await this.configurationService.retrieveTableData().toPromise(); 
    }
    

    [ts] Type 'Promise<any>' is not assignable to type 'boolean'

    编辑

    通过执行以下操作成功地消除了上述错误:

    public checkExistence(value: string): boolean{
      var exist = false;
      this.getRefData().then((rec: string[]) => {
        return rec.some(el => {
            return el === value;
        });
      });
    }
    

    现在的问题是函数实际上没有返回任何内容 [ts] A function whose declared type is neither 'void' nor 'any' must return a value.

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sandeep Gupta    7 年前

    checkExistence 回报是一种承诺 boolean

    public checkExistence(value: string): Promise<boolean>{
      var exist = false;
      return this.getRefData().then((rec: string[]) => {
        return rec.some(el => {
            el === value;
        });
      });
    }
    

    使用支票

    checkExistence()
       .then((value:boolean)=>{console.log(value)})
    
    推荐文章