Skip to content

Instantly share code, notes, and snippets.

@Sebmaster
Last active October 4, 2017 16:42
Show Gist options
  • Save Sebmaster/c19a8742143c9b53e0426ecec38b4969 to your computer and use it in GitHub Desktop.
Save Sebmaster/c19a8742143c9b53e0426ecec38b4969 to your computer and use it in GitHub Desktop.
How domains can be emulated with async_hooks
const hooks = async_hooks.create()
setTimeout(() => { // some async task
setImmediate(() => { // another one
throw new Error(); // woops!
})
}, 1000)
/*
With hooks:
init() trigger: 1, id: 2
before() id: 2
init() trigger: 2, id: 3
after() id: 2
before() id: 3
// uncaught exception. Kills the process
*/
/*
With hooks and uncaught exception handler:
init() trigger: 1, id: 2
before() id: 2 // register uncaught exception handler
init() trigger: 2, id: 3
after() id: 2 // deregister uncaught exception handler
destroy() id: 2 // don't really care about this one
before() id: 3 // register uncaught exception handler
process.on('uncaughtException') // oh, it's by id 3 since that's our last entered state
// it was triggered by id 2 -> 1. let's throw the error at 1!
after() id: 3
destroy() id: 3 // don't really care about this one
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment