Skip to content

Instantly share code, notes, and snippets.

@eskim
Created January 6, 2015 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eskim/feb90869227037777aac to your computer and use it in GitHub Desktop.
Save eskim/feb90869227037777aac to your computer and use it in GitHub Desktop.
import Fiber from 'fibers';
function sleep(ms) {
var fiber = Fiber.current;
setTimeout(function() {
// fiber.run();
// fiber.throwInto(new Error('throw!'));
setTimeout(_ => {
if(fiber.stopped){
fiber.throwInto('throw!');
}else{
fiber.run();
}
});
}, ms);
Fiber.yield();
}
class Foo {
bar(){
console.log('baz');
}
}
new Foo().bar();
var f = Fiber(_ =>{
try {
sleep(3000);
console.log('first');
sleep(2000);
console.log('second');
} catch(e){
console.log('caught in', e);
}
});
var f2 = Fiber(_ =>{
try {
sleep(3000);
console.log('2 - first');
sleep(2000);
console.log('2 - second');
} catch(e){
console.log('caught in', e);
}
});
setInterval(_ => { console.log('.'); }, 1000);
setTimeout(_ => {
f.stopped = true;
console.log('stopped');
}, 0);
f.run();
f2.run();
// f.reset();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment