Skip to content

Instantly share code, notes, and snippets.

@bjouhier
Created January 25, 2017 07:55
Show Gist options
  • Save bjouhier/ab384311dc10d67e2599b367eecc3fa2 to your computer and use it in GitHub Desktop.
Save bjouhier/ab384311dc10d67e2599b367eecc3fa2 to your computer and use it in GitHub Desktop.
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(1000, 1);
sleep(1000, 2);
}).run();
setInterval(function() {
console.log("tick");
}, 200);
@bjouhier
Copy link
Author

Result:

$ node fibers-sleep-sequential
1: sleep begin
tick
tick
tick
tick
wake up
1: sleep done
2: sleep begin
tick
tick
tick
tick
tick
wake up
2: sleep done
tick
tick
tick
^C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment