简体   繁体   English

赶不上承诺

[英]Catch Promise Rejection

I am running a test using WebdriverIO and precisely on this line: 我正在使用WebdriverIO进行测试,并且精确地在此行上:

await browser.waitForVisible('#tx-sent li', 15000)

Every now and then, I get a Promise rejection error: 时不时地,我收到一个Promise拒绝错误:

Error: Promise was rejected with the following reason: java.net.SocketException: Connection reset by peer (connect failed) 错误:由于以下原因拒绝了Promise:java.net.SocketException:对等重置连接(连接失败)

Is there a way to catch this promise rejection so that it doesn't cause the entire test to fail? 有没有一种方法可以捕获此承诺拒绝,从而不会导致整个测试失败? In other words I want to catch this Promise rejection and resolve it. 换句话说,我想抓住这个Promise拒绝并解决它。

You can use try/catch 您可以使用try / catch

try {
        await browser.waitForVisible('#tx-sent li', 15000);
} catch(e) {
        console.log(e);
}

You can use try and catch to handle errors in promises. 您可以使用try and catch来处理promise中的错误。 do something like this 做这样的事情

try {
   await browser.waitForVisible('#tx-sent li', 15000)
   } catch(error) {
  // thro or log erro as per you need
  //throw error;
   console.log(error);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM