Skip to content

Instantly share code, notes, and snippets.

@rgarcia
Created December 22, 2012 05:41
Show Gist options
  • Save rgarcia/4357652 to your computer and use it in GitHub Desktop.
Save rgarcia/4357652 to your computer and use it in GitHub Desktop.
playing with Node.js domains
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