I have an async function web3.eth.net.isListening()
that causes the following statement to be stuck forever if there is an error:
await web3.eth.net.isListening()
Question: How can we let the above await
statement timeout after 10 seconds, and do a console.log
to show that an error has occured?
You can use Promise.race()
to provide a second Promise that is tied to a timeout.
await Promise.race([
web3.eth.net.isListening(),
new Promise(function(resolve) {
setTimeout(function() {
console.log('Timed out');
resolve();
}, 10000);
}),
]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.