1

I would like to stop (gracefully or not) a function run via fibers. Fibers have throwInto() and reset() methods for doing that. But when I use it, my callback restart. Do you know why ?

Here a little example for what I have done.

Code:

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

Output :

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
  • Alter many test, I found one solution. I rethrow the error in the catch part. It's ugly but it's work. Do you have any better solutions ? – Boris SABATIER May 24 '16 at 07:23

0 Answers0