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] async_resource:与异步工作关联的可选对象,它将被传递给可能的async_hooksinit钩子。[in] async_resource_name:用于标识async_hooksAPI 提供的诊断信息资源类型的标识符。[in] execute:本地函数应被调用以异步执行逻辑。给定的函数会从工作线程池中调用,并且可以与主事件循环线程并行执行。[in] complete:当异步逻辑完成或被取消时,将调用本地函数。所给函数会从主事件循环线程中调用。napi_async_complete_callback提供了更多细节。[in] data:用户提供的数据上下文。这将传回到 execute 和 complete 函数中。[out] result:napi_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 应该是一个以空字符结尾的 UTF-8 编码字符串。
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.