napi_async_init
napi_status napi_async_init(napi_env env,
napi_value async_resource,
napi_value async_resource_name,
napi_async_context* result) -
[in] env:调用 API 的环境。¥
[in] env: The environment that the API is invoked under. -
[in] async_resource:与异步工作关联的对象,将传递给可能的async_hooksinit钩子 并可由async_hooks.executionAsyncResource()访问。¥
[in] async_resource: Object associated with the async work that will be passed to possibleasync_hooksinithooks and can be accessed byasync_hooks.executionAsyncResource(). -
[in] async_resource_name:为async_hooksAPI 公开的诊断信息提供的资源类型的标识符。¥
[in] async_resource_name: Identifier for the kind of resource that is being provided for diagnostic information exposed by theasync_hooksAPI. -
[out] result:初始化的异步上下文。¥
[out] result: The initialized async context.
如果 API 成功,则返回 napi_ok。
¥Returns napi_ok if the API succeeded.
为了保持与以前版本的 ABI 兼容性,为 async_resource 传递 NULL 不会导致错误。但是,不建议这样做,因为这会导致 async_hooks init 钩子 和 async_hooks.executionAsyncResource() 出现不良行为,因为底层 async_hooks 实现现在需要该资源,以便提供异步回调之间的链接。
¥In order to retain ABI compatibility with previous versions, passing NULL
for async_resource does not result in an error. However, this is not
recommended as this will result in undesirable behavior with async_hooks
init hooks and async_hooks.executionAsyncResource() as the resource is
now required by the underlying async_hooks implementation in order to provide
the linkage between async callbacks.
此 API 的早期版本在 napi_async_context 对象存在时不会维护对 async_resource 的强引用,而是期望调用者持有强引用。此事件已更改,因为在任何情况下,每次调用 napi_async_init() 时都必须相应地调用 napi_async_destroy,以避免内存泄漏。
¥Previous versions of this API were not maintaining a strong reference to
async_resource while the napi_async_context object existed and instead
expected the caller to hold a strong reference. This has been changed, as a
corresponding call to napi_async_destroy for every call to
napi_async_init() is a requirement in any case to avoid memory leaks.