1

我想停止(优雅地或不优雅地)通过光纤运行的功能。Fibers 有 throwInto() 和 reset() 方法来做到这一点。但是当我使用它时,我的回调重新启动。你知道为什么吗 ?

这是我所做的一个小例子。

代码:

Fiber = require("fibers")
f = Fiber(function(){
  try{
    console.log("First Wait 1000 sec");
    fiber = Fiber.current
    setTimeout(function(){
      fiber.run()
    }, 1000);
    Fiber.yield()
    console.log("End of first wait");
    console.log("Second Wait 1000 sec");
    fiber = Fiber.current
    setTimeout(function(){
      fiber.run()
    }, 1000);
    Fiber.yield()
    console.log("End of second wait");
  }catch(err){
    console.log("Got an error");
    console.log(err);
    console.log("Third Wait 1000 sec");
    fiber = Fiber.current
    setTimeout(function(){
      fiber.run()
    }, 1000);
    Fiber.yield()
    console.log("End of third wait");
  }
  return 0;
});

f.run();

setTimeout(function(){
  f.throwInto(new Error("End gracefully"));
  //f.reset(); //End brutally
}, 500);

输出 :

First Wait 1000 sec
Got an error
[Error: End gracefully]
Third Wait 1000 sec
End of third wait
First Wait 1000 sec
End of first wait
Second Wait 1000 sec
End of second wait
4

0 回答 0