函数未返回期望值打字稿

时间:2019-05-25 21:47:08

标签: angular typescript

我有一个通过api调用检查库存的函数,并且正在将其调用到另一个函数中。但这总是在执行apimethod.post之前返回true,好像apimethod.post在if条件之后求值。 有什么帮助吗?

public IsStockAvailable = (): any => {
  return this.apimethod.post(url, body, true).subscribe((response: any) => {
    if (response.RCode == AppConstant.ResponseStatus.StockError) {
      return false;
    }
    if (response.RCode == AppConstant.ResponseStatus.Success) {
      return true;
    }
  })
}

AddItem(){
if(!IsStockAvailable ){
    return;
  }
   // other part of code
}

1 个答案:

答案 0 :(得分:0)

您必须在if语句中调用函数

if(!IsStockAvailable() ){
    return;
}

否则,您只需将函数定义传递给if语句,这是事实!!