I have an async function web3.eth.isListening()
that causes the following statement to be stuck forever if there is an error:
await web3.eth.isListening()
How can we let the above await
statement timeout after 10 seconds, and do a console.log
to show that an error has occurred?
I have an async function web3.eth.isListening()
that causes the following statement to be stuck forever if there is an error:
await web3.eth.isListening()
How can we let the above await
statement timeout after 10 seconds, and do a console.log
to show that an error has occurred?
1 Answer
Reset to default 9You can use Promise.race()
to provide a second Promise that is tied to a timeout.
await Promise.race([
web3.eth.isListening(),
new Promise(function(resolve) {
setTimeout(function() {
console.log('Timed out');
resolve();
}, 10000);
}),
]);