is an object that represents the actual async resource that has been initialized.
This can contain useful information that can vary based on the value of
For instance, for the resource type, provides the hostname used when looking up the IP address for the hostname in
The API for accessing this information is currently not considered public, but using the Embedder API, users can provide and document their own resource objects.
For example, such a resource object could contain the SQL query being executed.
In the case of Promises, the object will have property that refers to the that is being initialized, and an property, set toif the promise has a parent promise, and otherwise.
For example, in the case of is considered a parent of
Here, is considered a chained promise.
In some cases the resource object is reused for performance reasons, it is thus not safe to use it as a key in a or add properties to it.
Asynchronous context example
The following is an example with additional information about the calls to between the and calls, specifically what the callback to will look like.
The output formatting is slightly more elaborate to make calling context easier to see.
Let's wait 10ms before logging the server started.
Output from only starting the server:
As illustrated in the example, and each specify the value of the current execution context; which is delineated by calls to and
Only using to graph resource allocation results in the following:
The is not part of this graph, even though it was the reason for being called.
This is because binding to a port without a hostname is a operation, but to maintain a completely asynchronous API the user's callback is placed in a
The graph only shows when a resource was created, not why, so to track the why use
When an asynchronous operation is initiated (such as a TCP server receiving a new connection) or completes (such as writing data to disk) a callback is called to notify the user.
The callback is called just before said callback is executed.
is the unique identifier assigned to the resource about to execute the callback.
The callback will be called 0 to N times.
The callback will typically be called 0 times if the asynchronous operation was cancelled or, for example, if no connections are received by a TCP server.
Persistent asynchronous resources like a TCP server will typically call the callback multiple times, while other operations like will call it only once.
Called immediately after the callback specified in is completed.
If an uncaught exception occurs during execution of the callback, then will run the event is emitted or a handler runs.
Called after the resource corresponding to is destroyed.
It is also called asynchronously from the embedder API
Some resources depend on garbage collection for cleanup, so if a reference is made to the object passed to it is possible that will never be called, causing a memory leak in the application.
If the resource does not depend on garbage collection, then this will not be an issue.
Called when the function passed to the constructor is invoked (either directly or through other means of resolving a promise).
Note that does not do any observable synchronous work.
The is not necessarily fulfilled or rejected at this point if the was resolved by assuming the state of another
calls the following callbacks:
Returns: The of the current execution context.
Useful to track when something calls.
The ID returned from is related to execution timing, not causality (which is covered by):
Returns the ID of the server, not of the new connection, because the callback runs in the execution scope of the server's
Returns the ID of a because all callbacks passed to are wrapped in a
Note that promise contexts may not get precise by default.
See the section on
The ID of the resource responsible for calling the callback that is currently being executed.
The resource that caused (or triggered) this callback to be called was that of the new connection.
Thus the return value of is the of
Even though all callbacks passed to are wrapped in a the callback itself exists because the call to the server's was made.
Note that promise contexts may not get valid by default.
By default, promise executions are not assigned due to the relatively expensive nature of the provided by
This means that programs using promises or will not get correct execution and trigger ids for promise callback contexts by default.
Here's an example:
Observe that the callback claims to have executed in the context of the outer scope even though there was an asynchronous hop involved.
Also note that the value is, which means that we are missing context about the resource that caused (triggered) the callback to be executed.
Installing async hooks via enables promise execution tracking.
forces PromiseHooks to be enabled.
In this example, adding any actual hook function enabled the tracking of promises.
There are two promises in the example above; the promise created by and the promise returned by the call to
In the example above, the first promise got the and the latter got
During the execution of the callback, we are executing in the context of promise with
This promise was triggered by async resource
Another subtlety with promises is that and callbacks are run only on chained promises.
That means promises not created by will not have the and callbacks fired on them.
For more details see the details of the V8 API.
The class is designed to be extended by the embedder's async resources.
Using this, users can easily trigger the lifetime events of their own resources.
The hook will trigger when an is instantiated.
The following is an overview of the API.
is meant to be extended.
Instantiating a new also triggers init.
If triggerAsyncId is omitted then is used.
Run a function in the execution context of the resource.
// * establish the context of the resource
// * trigger the callbacks
// * call the provided function with the supplied arguments
// * trigger the callbacks
// * restore the original execution context
Return the unique ID assigned to the AsyncResource instance.
Deprecated: Use instead.
The type of async event.
The ID of the execution context that created this async event.
Disables automatic when the object is garbage collected.
This usually does not need to be set (even if is called manually), unless the resource's is retrieved and the sensitive API's is called with it.
The function to call in the execution context of this async resource.
The receiver to be used for the function call.
Optional arguments to pass to the function.
Call the provided function with the provided arguments in the execution context of the async resource. This will establish the context, trigger the, call the function, trigger the, and then restore the original execution context.
Call all to notify that a new asynchronous execution context is being entered.
If nested calls to are made, the stack of will be tracked and properly unwound.
and calls must be unwound in the same order that they are called.
Otherwise, an unrecoverable exception will occur and the process will abort.
For this reason, the and APIs are considered deprecated.
Please use, as it provides a much safer alternative.
If nested calls to were made, then make sure the stack is unwound properly.
Otherwise an error will be thrown.
If the user's callback throws an exception, will automatically be called for all on the stack if the error is handled by a domain or handler.
This should only ever be called once.
An error will be thrown if it is called more than once.
This must be manually called.
If the resource is left to be collected by the GC then the will never be called.
The same that is passed to the constructor.