1

My application is dealing with high speed RPC requests, so it cannot reply on synchronous blocking call to process requests. It wants web3j RPC calls to return tx hash immediately without blocking and invoke callback later after RPC completion. Can web3j do callback as follows as in web3js?

web3.eth.sendTransaction({data: code}, function(err, transactionHash) {
  if (!err)
    console.log(transactionHash); // "0x7f9fade1c0d57a7af66ab4ead7c2eb7b11a91385"
});
sinoTrinity
  • 377
  • 2
  • 8

1 Answers1

2

You can use Promise. First create a promise object then execute task inside the promise. The following code will solve the problem.

var TxPromsie = new Promise(function(resolve, reject) {  
                var tx = web3.eth.sendTransaction({data: code});  
                if (tx != null) {  
                   resolve(tx);  
                } else {  
                   reject('could not get transaction data');  
                }  
            });  
            TxPromsie.then(function(value) {  
               console.log(value);  
            }).catch(function(error) {  
                console.log(error);  
             });  

This might help you!

Gopal ojha
  • 2,259
  • 2
  • 11
  • 21