0

I'm trying to run a function that sets up a bunch of things for my code. The first thing is getting a JSON object from an api to use later. When I run the code and console log the abi in the getAbi function, it logs out the JSON.

When I do the same thing from another function it returns undefined.

How can I fix this? I assume it's because the code is running before its returned, but I can't figure out how to get it in order.

async function getAbi (contract) {
    
    if (config['abi'] != ""){
        abi = config['abi']
        return abi
    }else {
        request('https://api.etherscan.com/api?module=contract&action=getabi&address='+ contract + '&apikey=' + bscScanToken, async (err, response, body) => {
            if (err) {
                console.log(err)
            } else{ 
                body = JSON.parse(body)
                abi = body.result
                return abi
            }
        })
    }
}

async function setupMint() {
    abi = await getAbi(contract)
    console.log(abi)
  • The "else" part of getAbi doesn't return anything. `return abi` is returning from the function specified with `async (err, response, body) =>` – James Jan 28 '22 at 19:01
  • Thank you James. I've fixed that and moved it down into the else function, but I'm still getting the same result – profoundwatcher Jan 28 '22 at 19:06

0 Answers0