Javascript中的承诺与“ .then()”一起使用,但与异步/等待不一起使用

时间:2020-06-29 16:58:51

标签: javascript async-await protractor es6-promise

函数调用:

var oldBalance =  objToPageObjectFile.getBalance();
//objToPageObjectFile is object referring to a file where "getBalance()" is defined.

功能定义:

Works with ".then()"

 this.getBalance=function()
 {
    var storeBalance = readBalance.getText().then(function(balance){
        return balance;
 });

    return storeBalance;
}

不适用于异步/等待

this.getBalance() = async function()
{
    var storeBalance;
    storeBalance = await readBalance.getText();
    return storeBalance;
}

错误是: 错误:TypeError:this.getBalance不是函数

为什么我的异步/等待错误?我正在使用InteliJ IDE。

1 个答案:

答案 0 :(得分:0)

问题可能是getBalance之后的括号,请尝试以下操作:

this.getBalance = async () => {
  // Your code goes here
}

更新

编辑了代码,以重点解决问题中的错误。