Instantly share code, notes, and snippets.

Embed
What would you like to do?
var Fiber = require('fibers');
function sleep(millis, id) {
var fiber = Fiber.current;
console.log(id + ": sleep begin");
setTimeout(function() {
console.log("wake up");
fiber.run();
}, millis)
Fiber.yield();
console.log(id + ": sleep done");
}
Fiber(function() {
sleep(2000, 1);
}).run();
Fiber(function() {
sleep(1000, 2);
}).run();
setInterval(function() {
console.log("tick");
}, 200);
@bjouhier

This comment has been minimized.

Owner

bjouhier commented Jan 25, 2017

Result:

$ node fibers-sleep-parallel
1: sleep begin
2: sleep begin
tick
tick
tick
tick
wake up
2: sleep done
tick
tick
tick
tick
tick
wake up
1: sleep done
tick
tick
tick
^C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment