【问题标题】:How to loop async code with a try-catch block inside using generators?如何使用生成器在内部使用 try-catch 块循环异步代码?
【发布时间】:2013-07-16 09:58:05
【问题描述】:

这个简单的代码只会输入一次捕获。 如何用 try catch 循环这个?

run(function* (gen) {
  while (true) {
    try {
      yield request('http://www.unexistanturl.com', gen());
    } catch (err) {
      debug(err);
    }
  }
})

【问题讨论】:

    标签: javascript node.js generator v8


    【解决方案1】:
    run(function* (gen) {
      while (true) {
          yield function(){
            try {
              request('http://www.unexistanturl.com', gen());
            } catch (err) {
              debug(err);
            }
          }();
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2021-09-11
      • 2022-12-11
      • 2020-04-08
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 2016-10-01
      • 2017-11-10
      相关资源
      最近更新 更多