I use the package fibersfor node.js. Suppose I create several fibers and want to wait in the main thread to complete any of them. The API for an object Fiberlists only non-blocking functions, such as run. The object Futureis not documented at all. So how should this be done? Here is an example program (in which there is no function definition waitForSomeFiberToFinish):
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();
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');
source
share