Promise in Javascript 使用“.then()”但不使用 async/await
Promise in Javascript working with ".then()" but not with async/await
函数调用:
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;
}
不适用于 async/await
this.getBalance() = async function()
{
var storeBalance;
storeBalance = await readBalance.getText();
return storeBalance;
}
错误是:
错误:类型错误:this.getBalance 不是函数
为什么我在使用 async/await 时出错?我正在使用 InteliJ IDE.
问题可能出在 getBalance
之后的括号上,试试这个:
this.getBalance = async () => {
// Your code goes here
}
更新
编辑了代码以重点解决问题中的错误。
函数调用:
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;
}
不适用于 async/await
this.getBalance() = async function()
{
var storeBalance;
storeBalance = await readBalance.getText();
return storeBalance;
}
错误是: 错误:类型错误:this.getBalance 不是函数
为什么我在使用 async/await 时出错?我正在使用 InteliJ IDE.
问题可能出在 getBalance
之后的括号上,试试这个:
this.getBalance = async () => {
// Your code goes here
}
更新
编辑了代码以重点解决问题中的错误。