How to use async_hooks - 10 common examples

To help you get started, we’ve selected a few async_hooks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github graalvm / graaljs / test / async-hooks / test-embedder.api.async-resource.js View on Github external
common.expectsError(
  () => new AsyncResource(), {
    code: 'ERR_INVALID_ARG_TYPE',
    type: TypeError,
  });
common.expectsError(() => {
  new AsyncResource('invalid_trigger_id', { triggerAsyncId: null });
}, {
  code: 'ERR_INVALID_ASYNC_ID',
  type: RangeError,
});

assert.strictEqual(
  new AsyncResource('default_trigger_id').triggerAsyncId(),
  async_hooks.executionAsyncId()
);

// create first custom event 'alcazares' with triggerAsyncId derived
// from async_hooks executionAsyncId
const alcaTriggerId = async_hooks.executionAsyncId();
const alcaEvent = new AsyncResource('alcazares', alcaTriggerId);
const alcazaresActivities = hooks.activitiesOfTypes([ 'alcazares' ]);

// alcazares event was constructed and thus only has an `init` call
assert.strictEqual(alcazaresActivities.length, 1);
const alcazares = alcazaresActivities[0];
assert.strictEqual(alcazares.type, 'alcazares');
assert.strictEqual(typeof alcazares.uid, 'number');
assert.strictEqual(alcazares.triggerAsyncId, alcaTriggerId);
checkInvocations(alcazares, { init: 1 }, 'alcazares constructed');
github thlorenz / hookup / exercises / 02-init.timer-enable-late.js View on Github external
const asyncHooks = require('async_hooks')
const print = require('../lib/print')

function init(id, type, triggerAsyncId, resource) {
  print({ stage: 'init', id, type, triggerAsyncId })
}

const hook = asyncHooks.createHook({ init })
setTimeout(() => {}, 10)

// TODO:
// Here we don't see anything printed. Do you know why?
hook.enable()
github cheneyweb / nodetracing / client / nodetracing_modules / nodetracing / src / Instrument.js View on Github external
async function afterAsync(res) {
            let contextMap = Instrument.contextMap
            let context = contextMap.get(asyncHooks.executionAsyncId())
            // let id = asyncHooks.executionAsyncId()
            await res
            // console.log(`after ${context.span.operationName}:${id}`)
            context.span.finish()
            return res
        }
        function after(res) {
github tapjs / async-hook-domain / index.js View on Github external
constructor (onerror) {
    if (typeof onerror !== 'function') {
      // point at where the wrong thing was actually done
      const er = new TypeError('onerror must be a function')
      Error.captureStackTrace(er, this.constructor)
      throw er
    }
    const eid = executionAsyncId()
    this.ids = new Set([eid])
    this.onerror = onerror
    this.parent = domains.get(executionAsyncId())
    this.destroyed = false
    domains.set(eid, this)
    activateDomains()
  }
github graalvm / graaljs / test / parallel / test-async-hooks-recursive-stack.js View on Github external
a.runInAsyncScope(() => {
    assert.strictEqual(a.asyncId(), async_hooks.executionAsyncId());
    assert.strictEqual(a.triggerAsyncId(), async_hooks.triggerAsyncId());
    if (n >= 0)
      recurse(n - 1);
    assert.strictEqual(a.asyncId(), async_hooks.executionAsyncId());
    assert.strictEqual(a.triggerAsyncId(), async_hooks.triggerAsyncId());
  });
}
github graalvm / graaljs / test / parallel / test-async-hooks-promise-triggerid.js View on Github external
Promise.resolve(42).then(common.mustCall(() => {
  assert.strictEqual(async_hooks.executionAsyncId(), promiseAsyncIds[1]);
  assert.strictEqual(async_hooks.triggerAsyncId(), promiseAsyncIds[0]);
  Promise.resolve(10);
}));
github graalvm / graaljs / test / parallel / test-async-wrap-uncaughtexception.js View on Github external
process.on('uncaughtException', common.mustCall(() => {
  assert.strictEqual(call_id, async_hooks.executionAsyncId());
  call_log[2]++;
}));
github graalvm / graaljs / test / parallel / test-async-hooks-run-in-async-id-scope.js View on Github external
async_hooks.runInAsyncIdScope(asyncId, common.mustCall(() => {
  assert.strictEqual(async_hooks.executionAsyncId(), asyncId);
}));
github bmeurer / async-hooks-performance-impact / async-hook-init.js View on Github external
const async_hooks = require('async_hooks');
const hook = async_hooks.createHook({
    init(asyncId, type, triggerAsyncId) { }
});
hook.enable();
github guyguyon / node-request-context / index.js View on Github external
function createHooks(namespace) {
    function init(asyncId, type, triggerId, resource) {
        if (namespace.context[triggerId]) {
            namespace.context[asyncId] = namespace.context[triggerId];
        }
    }

    function destroy(asyncId) {
        delete namespace.context[asyncId];
    }

    const asyncHook = asyncHooks.createHook({ init, destroy });

    asyncHook.enable();
}

async_hooks

squat for node core module

Unknown
Latest version published 8 years ago

Package Health Score

39 / 100
Full package analysis

Similar packages