温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/async_hooks/createHook
点击访问原文链接

Node async_hooks.createHook function | API Reference | Bun

Node async_hooks.createHook function | API Reference | BunBuildDocsReferenceGuidesBlogDiscord/node:async_hooks/createHookFcreateHook

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:async_hooks/createHookFcreateHook

function

async_hooks.createHookfunction createHook(callbacks: HookCallbacks): AsyncHook;Registers functions to be called for different lifetime events of each async operation.

The callbacks init()/before()/after()/destroy() are called for the respective asynchronous event during a resource's lifetime.

All callbacks are optional. For example, if only resource cleanup needs to be tracked, then only the destroy callback needs to be passed. The specifics of all functions that can be passed to callbacks is in the Hook Callbacks section.

import { createHook } from 'node:async_hooks'; const asyncHook = createHook({ init(asyncId, type, triggerAsyncId, resource) { }, destroy(asyncId) { }, }); The callbacks will be inherited via the prototype chain:

class MyAsyncCallbacks { init(asyncId, type, triggerAsyncId, resource) { } destroy(asyncId) {} } class MyAddedCallbacks extends MyAsyncCallbacks { before(asyncId) { } after(asyncId) { } } const asyncHook = async_hooks.createHook(new MyAddedCallbacks()); Because promises are asynchronous resources whose lifecycle is tracked via the async hooks mechanism, the init(), before(), after(), anddestroy() callbacks must not be async functions that return promises.

@param callbacksThe Hook Callbacks to register

@returnsInstance used for disabling and enabling hooks

Referenced typesinterface HookCallbacksafter(asyncId: number): void;Called immediately after the callback specified in before is completed.

If an uncaught exception occurs during execution of the callback, then after will run after the 'uncaughtException' event is emitted or a domain's handler runs.

@param asyncIdthe unique identifier assigned to the resource which has executed the callback.

before(asyncId: number): void;When an asynchronous operation is initiated or completes a callback is called to notify the user. The before callback is called just before said callback is executed.

@param asyncIdthe unique identifier assigned to the resource about to execute the callback.

destroy(asyncId: number): void;Called after the resource corresponding to asyncId is destroyed

@param asyncIda unique ID for the async resource

init(asyncId: number,type: string,triggerAsyncId: number,resource: object): void;Called when a class is constructed that has the possibility to emit an asynchronous event.

@param asyncIdA unique ID for the async resource

@param typeThe type of the async resource

@param triggerAsyncIdThe unique ID of the async resource in whose execution context this async resource was created

@param resourceReference to the resource representing the async operation, needs to be released during destroy

promiseResolve(asyncId: number): void;Called when a promise has resolve() called. This may not be in the same execution id as the promise itself.

@param asyncIdthe unique id for the promise that was resolve()d.

interface AsyncHookdisable(): this;Disable the callbacks for a given AsyncHook instance from the global pool of AsyncHook callbacks to be executed. Once a hook has been disabled it will not be called again until enabled.

enable(): this;Enable the callbacks for a given AsyncHook instance. If no callbacks are provided enabling is a noop.

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco

We're hiring →

智能索引记录