Skip to content

Commit 838179b

Browse files
FlarnaRafaelGSS
authored andcommitted
doc: correct tracingChannel.traceCallback()
tracingChannel.traceCallback() requires a callback otherwise it throws and invalid argument error. As a result arguments are not optional. Correct the documentation to reflect that arguments are not optional. Besides that correct description regarding signaling of errors. Remove an unneeded null check in wrappedCallback() which can't happen because it's validated that callback is of type function. PR-URL: #51068 Fixes: #50996 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
1 parent 539bee4 commit 838179b

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

doc/api/diagnostics_channel.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ channels.tracePromise(async () => {
853853
});
854854
```
855855

856-
#### `tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])`
856+
#### `tracingChannel.traceCallback(fn, position, context, thisArg, ...args)`
857857

858858
<!-- YAML
859859
added:
@@ -864,23 +864,23 @@ added:
864864
865865
* `fn` {Function} callback using function to wrap a trace around
866866
* `position` {number} Zero-indexed argument position of expected callback
867-
* `context` {Object} Shared object to correlate trace events through
867+
(defaults to last argument if `undefined` is passed)
868+
* `context` {Object} Shared object to correlate trace events through (defaults
869+
to `{}` if `undefined` is passed)
868870
* `thisArg` {any} The receiver to be used for the function call
869-
* `...args` {any} Optional arguments to pass to the function
871+
* `...args` {any} arguments to pass to the function (must include the callback)
870872
* Returns: {any} The return value of the given function
871873

872-
Trace a callback-receiving function call. This will always produce a
874+
Trace a callback-receiving function call. The callback is expected to follow
875+
the error as first arg convention typically used. This will always produce a
873876
[`start` event][] and [`end` event][] around the synchronous portion of the
874877
function execution, and will produce a [`asyncStart` event][] and
875878
[`asyncEnd` event][] around the callback execution. It may also produce an
876-
[`error` event][] if the given function throws an error or the returned
877-
promise rejects. This will run the given function using
879+
[`error` event][] if the given function throws or the first argument passed to
880+
the callback is set. This will run the given function using
878881
[`channel.runStores(context, ...)`][] on the `start` channel which ensures all
879882
events should have any bound stores set to match this trace context.
880883

881-
The `position` will be -1 by default to indicate the final argument should
882-
be used as the callback.
883-
884884
```mjs
885885
import diagnostics_channel from 'node:diagnostics_channel';
886886

lib/diagnostics_channel.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,7 @@ class TracingChannel {
371371
// Using runStores here enables manual context failure recovery
372372
asyncStart.runStores(context, () => {
373373
try {
374-
if (callback) {
375-
return ReflectApply(callback, this, arguments);
376-
}
374+
return ReflectApply(callback, this, arguments);
377375
} finally {
378376
asyncEnd.publish(context);
379377
}

0 commit comments

Comments
 (0)