0

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()

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 1

9

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);
    }),
]);
1
  • 5
    Good idea to .race, but I would reject in your timeout.
    – Keith
    Nov 2, 2018 at 21:59

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.