[英]Connection timeouts in web3.js
When instantiating a Web3 instance, I can pass provider options, including the timeout, eg:实例化 Web3 实例时,我可以传递提供程序选项,包括超时,例如:
const w3 = new Web3(new Web3.providers.HttpProvider('...', {timeout: 10e3}));
But seems like it doesn't affect the isListening
call, when I'm trying to connect to an unhealthy node and checking if it's listening:但似乎它不会影响isListening
调用,当我尝试连接到不健康的节点并检查它是否正在侦听时:
await w3.eth.net.isListening();
is waiting for way more than the timeout I specify.正在等待超过我指定的超时时间。
Why is it like that?为什么会这样? How can I force the timeout there?我怎样才能在那里强制超时?
You can use Promise.race() to have a second promise that is tied to a timeout.您可以使用Promise.race()来获得与超时相关的第二个承诺。
await Promise.race([
web3.eth.net.isListening(),
new Promise(function(resolve, reject) {
setTimeout(function() {
reject("Time out");
}, 10e3);
})
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.