Created
December 22, 2012 05:41
-
-
Save rgarcia/4357652 to your computer and use it in GitHub Desktop.
playing with Node.js domains
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
domain = require 'domain' | |
bad = (msg, cb) -> | |
if (r = Math.random()) < 0.33 | |
cb null, msg | |
else if r < 0.66 | |
cb new Error('error via callback') | |
else | |
throw new Error('error via exception') | |
domain_main = domain.create() | |
domain_main.run -> | |
# errors via calling this right here right now don't get caught by the domain and cause a crash :( | |
# bad 'hello', domain_main.intercept((response) -> console.log response) | |
# only errors occuring after this tick get caught by the domain | |
setInterval () -> | |
bad 'hello', domain_main.intercept((response) -> console.log response) | |
, 1000 | |
domain_main.on "error", (error) -> | |
console.error error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment