How to use the async_hooks.executionAsyncId function in async_hooks

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 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 eBay / arc / packages / arc-server / index.js View on Github external
if (!hook) {
        hook = createHook({ 
            init(asyncId, type, triggerAsyncId) {
                const context = contexts.get(triggerAsyncId);
                if (context) {
                    contexts.set(asyncId, context);
                }
            },
            destroy(asyncId) {
                contexts.delete(asyncId);
            }
        })
        hook.enable();
    }

    const id = executionAsyncId();
    const oldValue = contexts.get(id);
    contexts.set(id, normalizeFlags(flags));
    try {
        fn();
    } finally {
        if (oldValue === undefined) {
            contexts.delete(id);
        } else {
            contexts.set(id, oldValue);
        }
    }
};
github faastjs / faast.js / src / trace.ts View on Github external
export function printAsyncStack() {
    console.log(`Async stack:`);
    let res = asyncObjects.get(asyncHooks.executionAsyncId());
    while (res) {
        const { stack, ...rest } = res;
        console.log(`%O\n${stack}`, rest);
        res = asyncObjects.get(res.triggerId);
    }
}
github guyguyon / node-request-context / namespace.js View on Github external
set(key, val) {
        const eid = asyncHooks.executionAsyncId();
        this.context[eid][key] = val;
    }

async_hooks

squat for node core module

Unknown
Latest version published 8 years ago

Package Health Score

39 / 100
Full package analysis

Similar packages