Created
January 6, 2015 01:42
-
-
Save eskim/feb90869227037777aac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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