在等待中等待不会给出正确的输出

时间:2019-06-30 05:58:10

标签: javascript asynchronous async-await ethereum web3

更新:以下是我的代码,我要检查令牌是否存在。如果是,那么我将检查钱包所有者是否是令牌所有者。现在的问题是,它没有检查第二个函数“ contract.methods.ownerOf(tokenId).call(function(err,res)”),因此最终结果不是正确的结果。

async function doesTokenIdExist(tokenId, contract, walletAddress) {

    var tokenExists = false;

    await contract.methods.exists(tokenId).call(async function (err, res) {

        if (res) {

            await contract.methods.ownerOf(tokenId).call(function (err, res) {

                if (!err) {

                    tokenAddress = res.toLowerCase();
                    walletAddress = walletAddress.toLowerCase();

                    if (tokenAddress.localeCompare(walletAddress) == 0){

                        tokenExists = true;

                    } else {
                        tokenExists = false;
                    }

                } else {

                    tokenExists = false;

                }

            });


        } else {
            tokenExists = false;

        }


    });

    return tokenExists;

}

1 个答案:

答案 0 :(得分:4)

更改此内容

await contract.methods.exists(tokenId).call(function (err, res) {

对此,

await contract.methods.exists(tokenId).call(async function (err, res) {
相关问题