I have the following code that waits for a transaction to get mined on the Ethereum blockchain.
function waitForMinedTransaction(txHash, tries = 1) {
return new Promise(function(resolve, reject) {
web3.eth.getTransactionReceipt(txHash, function(err, res) {
if (err) reject(err)
if (res) resolve(res)
// nothing yet (retry in 10 sec..)
console.log(`Attempt #${ tries }...`)
if (tries > 60) reject("max_tries_exceeded")
setTimeout(function() { return waitForMinedTransaction(txHash, tries + 1) }, 10000)
})
})
}
The issue is that when the transaction is mined (e.g. after 10 tries), it never gets resolved. I'm sure this has something to do with setTimeout and the promise chain (where a Promise is returned instead of resolve/rejecting the current promise) but need some pointers on fixing it.