async_hooks: docs and api ergonomics tweaks by jasnell · Pull Request #15103 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 85 additions & 47 deletions doc/api/async_hooks.md
3 changes: 3 additions & 0 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ function triggerAsyncId() {

class AsyncResource {
constructor(type, triggerAsyncId = initTriggerId()) {
if (typeof type !== 'string')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'type', 'string');

// Unlike emitInitScript, AsyncResource doesn't supports null as the
// triggerAsyncId.
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
Expand Down
34 changes: 34 additions & 0 deletions test/async-hooks/test-embedder.api.async-resource-no-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const { AsyncResource } = async_hooks;
const { spawn } = require('child_process');

const initHooks = require('./init-hooks');

if (process.argv[2] === 'child') {
initHooks().enable();

class Foo extends AsyncResource {
constructor(type) {
super(type, async_hooks.executionAsyncId());
}
}

[null, undefined, 1, Date, {}, []].forEach((i) => {
common.expectsError(() => new Foo(i), {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
});

} else {
const args = process.argv.slice(1).concat('child');
spawn(process.execPath, args)
.on('close', common.mustCall((code) => {
// No error because the type was defaulted
assert.strictEqual(code, 0);
}));
}
11 changes: 5 additions & 6 deletions test/async-hooks/test-embedder.api.async-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ const { checkInvocations } = require('./hook-checks');
const hooks = initHooks();
hooks.enable();

assert.throws(() => {
new AsyncResource();
}, common.expectsError({
code: 'ERR_ASYNC_TYPE',
type: TypeError,
}));
common.expectsError(
() => new AsyncResource(), {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
});
assert.throws(() => {
new AsyncResource('invalid_trigger_id', null);
}, common.expectsError({
Expand Down