Search code examples
node.jspromisecallbackethereumweb3js

Callback isn't triggered node.js


Probabilly i'm missing some concepts of callbacks and promises here, but i can't find the way to my code works.

Here is it:

var tx;

web3.eth.getBlock(6339515, function(err, result){
    for(var i = 0; i <= result.transactions.length; i++){
        tx = result.transactions[i];
        getInputTransaction(tx)
        .then(function() {} )
        .catch(function(error) {
            console.log('error: \n' + error);
        });
    }
})

async function getInputTransaction(tx) {
    web3.eth.getTransaction(tx, function(err, cb){
        console.log('got here');
        let decodeInput = web3.utils.hexToAscii(cb.input);
        decodeInput = decodeInput.split("_").pop();
        if(!err){
            console.log(cb);
            console.log('\nInput decoded: ' + '\u001b[1;32m' + decodeInput + '\u001b[0m');
        }else{
            console.log('error: ' + error);
    }}
    )
}

Basically, i want get the result callback of first method to get each index value in pass it to the second method to scan this value, in this case is an ethereum transaction to get the input value. The problem is the callback named "cb" isn't triggered.

The respectives documentations:

getBlock getTransaction

What am i missing here?


Solution

  • Got it.

    The problem was i wasn't ensure the parameter cb in getInputTransaction

    With if(cb && cb.input != undefined) before let decodeInput = web3.utils.hexToAscii(cb.input) it works well.