Skip to content

Instantly share code, notes, and snippets.

@guyguyon
Last active March 11, 2018 11:21
Show Gist options
  • Select an option

  • Save guyguyon/66f5f15a38f16f38e56344bada49e002 to your computer and use it in GitHub Desktop.

Select an option

Save guyguyon/66f5f15a38f16f38e56344bada49e002 to your computer and use it in GitHub Desktop.
// node-request-context/index.js
const asyncHooks = require('async_hooks');
function createHooks(namespace) {
function init(asyncId, type, triggerId, resource) {
// Check if trigger has context
// Or in other words, check if tigger is part of any request
if (namespace.context[triggerId]) {
// Here we keep passing the context from
// the triggerId to the new asyncId
namespace.context[asyncId] = namespace.context[triggerId];
}
}
function destroy(asyncId) {
delete namespace.context[asyncId];
}
const asyncHook = asyncHooks.createHook({ init, destroy });
asyncHook.enable();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment