4

I use the fibers package for node.js. Suppose I create several fibers, and want to wait in the main thread for any one of them to finish. The API for the Fiber object only lists non-blocking functions like run. The Future object doesn't seem to be documented at all. So how is this supposed to be done? Here's a sample program (which lacks the definition of the waitForSomeFiberToFinish function):

var Fiber = require('fibers');

function sleep(ms) {
  var fiber = Fiber.current;
  setTimeout(function() {
    fiber.run();
  }, ms);
  Fiber.yield();
}

f1 = Fiber(function(){sleep(1000);});
f2 = Fiber(function(){sleep(2000);});

f1.run();
f2.run();

// The question is: how does one implement waitForSomeFiberToFinish?
waitForSomeFiberToFinish([f1, f2]);
console.log('We should have slept now for one second and at this point f1 is finished');
waitForSomeFiberToFinish([f1, f2]);
console.log('We should have slept now for another second and at this point f2 is finished');
nowk
  • 32,822
  • 2
  • 35
  • 40
dragonroot
  • 5,653
  • 3
  • 38
  • 63
  • Have you checked https://github.com/luciotato/waitfor ? Specifically https://github.com/luciotato/waitfor/blob/master/paralell-tests.js as samples of parallel fibers (parallel map). – Lucio M. Tato Apr 22 '14 at 18:16

0 Answers0