Await Needed Here?

Hi there,

I have a question about await in my .js code. When I create a gen0 kitty, in the browser console I first get the err message and after a few seconds when the instance gets the txHash back, it shows me the alert message. Should I implement “await” here, and if so, what’s the best way to properly do it? Thank you so much!!

function createCat(){

    var dnaStr = getDna();  

    instance.methods.createCatGen0(dnaStr).send({}, function(error, txHash){

        if (err)

            console.log(err);

        else

            console.log(txHash);

    })

}

hey @viktormascot! In order to use await your function needs to be async. Its always better to use async functions with await for this cases that you are expecting a result that you dont know when is gonna happen.
Example :


async function createCat(){
    var dnaStr = getDna();  
    //Also await on this method
    await instance.methods.createCatGen0(dnaStr).send({}, async function(error, txHash){
        if (err){
           console.log(err);
         } else {           
           console.log(txHash);
         }         
    } 
}
1 Like