Options
All
  • Public
  • Public/Protected
  • All
Menu

The class AsyncResource was designed to be extended by the embedder's async resources. Using this users can easily trigger the lifetime events of their own resources.

Hierarchy

Index

Constructors

constructor

  • AsyncResource() is meant to be extended. Instantiating a new AsyncResource() also triggers init. If triggerAsyncId is omitted then async_hook.executionAsyncId() is used.

    Parameters

    • type: string

      The type of async event.

    • Optional triggerAsyncId: number | AsyncResourceOptions

      The ID of the execution context that created this async event (default: executionAsyncId()), or an AsyncResourceOptions object (since 9.3)

    Returns AsyncResource

Methods

asyncId

  • asyncId(): number
  • Returns number

    the unique ID assigned to this AsyncResource instance.

bind

  • bind<Func>(fn: Func): Func & object
  • Binds the given function to execute to this AsyncResource's scope.

    Type parameters

    • Func: function

    Parameters

    • fn: Func

      The function to bind to the current AsyncResource.

    Returns Func & object

emitDestroy

  • emitDestroy(): void
  • Call AsyncHooks destroy callbacks.

    Returns void

runInAsyncScope

  • runInAsyncScope<This, Result>(fn: function, thisArg?: This, ...args: any[]): Result
  • Call the provided function with the provided arguments in the execution context of the async resource. This will establish the context, trigger the AsyncHooks before callbacks, call the function, trigger the AsyncHooks after callbacks, and then restore the original execution context.

    Type parameters

    • This

    • Result

    Parameters

    • fn: function

      The function to call in the execution context of this async resource.

        • (this: This, ...args: any[]): Result
        • Parameters

          • this: This
          • Rest ...args: any[]

          Returns Result

    • Optional thisArg: This

      The receiver to be used for the function call.

    • Rest ...args: any[]

      Optional arguments to pass to the function.

    Returns Result

triggerAsyncId

  • triggerAsyncId(): number
  • Returns number

    the trigger ID for this AsyncResource instance.

Static bind

  • bind<Func>(fn: Func, type?: string): Func & object
  • Binds the given function to the current execution context.

    Type parameters

    • Func: function

    Parameters

    • fn: Func

      The function to bind to the current execution context.

    • Optional type: string

      An optional name to associate with the underlying AsyncResource.

    Returns Func & object

Generated using TypeDoc