napi_create_async_work
napi_status napi_create_async_work(napi_env env,
napi_value async_resource,
napi_value async_resource_name,
napi_async_execute_callback execute,
napi_async_complete_callback complete,
void* data,
napi_async_work* result);
-
[in] env
:调用 API 的环境。¥
[in] env
: The environment that the API is invoked under. -
[in] async_resource
:与将传递给可能的async_hooks
init
钩子 的异步工作关联的可选对象。¥
[in] async_resource
: An optional object associated with the async work that will be passed to possibleasync_hooks
init
hooks. -
[in] async_resource_name
:为async_hooks
API 公开的诊断信息提供的资源类型的标识符。¥
[in] async_resource_name
: Identifier for the kind of resource that is being provided for diagnostic information exposed by theasync_hooks
API. -
[in] execute
:应调用以异步执行逻辑的原生函数。给定的函数从工作池线程调用,可以与主事件循环线程并行执行。¥
[in] execute
: The native function which should be called to execute the logic asynchronously. The given function is called from a worker pool thread and can execute in parallel with the main event loop thread. -
[in] complete
:异步逻辑完成或取消时将调用的原生函数。从主事件循环线程调用给定的函数。napi_async_complete_callback
提供了更多详细信息。¥
[in] complete
: The native function which will be called when the asynchronous logic is completed or is cancelled. The given function is called from the main event loop thread.napi_async_complete_callback
provides more details. -
[in] data
:用户提供的数据上下文。这将被传递回执行和完成功能。¥
[in] data
: User-provided data context. This will be passed back into the execute and complete functions. -
[out] result
:napi_async_work*
是新创建的异步工作的句柄。¥
[out] result
:napi_async_work*
which is the handle to the newly created async work.
如果 API 成功,则返回 napi_ok
。
¥Returns napi_ok
if the API succeeded.
此 API 分配用于异步执行逻辑的工作对象。一旦不再需要该工作,应使用 napi_delete_async_work
将其释放。
¥This API allocates a work object that is used to execute logic asynchronously.
It should be freed using napi_delete_async_work
once the work is no longer
required.
async_resource_name
应该是一个以 null 结尾的 UTF-8 编码字符串。
¥async_resource_name
should be a null-terminated, UTF-8-encoded string.
async_resource_name
标识符由用户提供,应该代表正在执行的异步工作的类型。还建议将命名空间应用于标识符,例如 通过包含模块名称。有关详细信息,请参阅 async_hooks
文档。
¥The async_resource_name
identifier is provided by the user and should be
representative of the type of async work being performed. It is also recommended
to apply namespacing to the identifier, e.g. by including the module name. See
the async_hooks
documentation for more information.