简体   繁体   English

Javascript 中的 Promise 使用“.then()”但不使用 async/await

[英]Promise in Javascript working with “.then()” but not with async/await

Function call: Function 致电:

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

Function definition: Function定义:

Works with ".then()"

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

    return storeBalance;
}

Not working with async/await不使用异步/等待

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

Error is: Error: TypeError: this.getBalance is not a function错误是:错误:TypeError:this.getBalance不是function

Why am I getting error with async/await?为什么我收到异步/等待错误? I am using InteliJ IDE.我正在使用 InteliJ IDE。

The problem could be the brackets after getBalance , try this:问题可能是getBalance之后的括号,试试这个:

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

Update更新

Edited the code to focus on the solution to the error in the question.编辑代码以专注于解决问题中的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM