Search code examples
javascriptasync-awaitprotractores6-promise

Promise in Javascript working with ".then()" but not with async/await


Function call:

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

Function definition:

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

Why am I getting error with async/await? I am using InteliJ IDE.


Solution

  • The problem could be the brackets after getBalance, try this:

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

    Update

    Edited the code to focus on the solution to the error in the question.