Skip to content

Commit 3f942e2

Browse files
authored
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 8cdb7ca commit 3f942e2

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
@@ -862,7 +862,7 @@ channels.tracePromise(async () => {
862862
});
863863
```
864864

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

867867
<!-- YAML
868868
added:
@@ -874,23 +874,23 @@ added:
874874
875875
* `fn` {Function} callback using function to wrap a trace around
876876
* `position` {number} Zero-indexed argument position of expected callback
877-
* `context` {Object} Shared object to correlate trace events through
877+
(defaults to last argument if `undefined` is passed)
878+
* `context` {Object} Shared object to correlate trace events through (defaults
879+
to `{}` if `undefined` is passed)
878880
* `thisArg` {any} The receiver to be used for the function call
879-
* `...args` {any} Optional arguments to pass to the function
881+
* `...args` {any} arguments to pass to the function (must include the callback)
880882
* Returns: {any} The return value of the given function
881883

882-
Trace a callback-receiving function call. This will always produce a
884+
Trace a callback-receiving function call. The callback is expected to follow
885+
the error as first arg convention typically used. This will always produce a
883886
[`start` event][] and [`end` event][] around the synchronous portion of the
884887
function execution, and will produce a [`asyncStart` event][] and
885888
[`asyncEnd` event][] around the callback execution. It may also produce an
886-
[`error` event][] if the given function throws an error or the returned
887-
promise rejects. This will run the given function using
889+
[`error` event][] if the given function throws or the first argument passed to
890+
the callback is set. This will run the given function using
888891
[`channel.runStores(context, ...)`][] on the `start` channel which ensures all
889892
events should have any bound stores set to match this trace context.
890893

891-
The `position` will be -1 by default to indicate the final argument should
892-
be used as the callback.
893-
894894
```mjs
895895
import diagnostics_channel from 'node:diagnostics_channel';
896896

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)