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_hooks init 钩子
  • [in] async_resource_name:用于标识通过 async_hooks API 提供的诊断信息的资源类型。
  • [in] execute:应该调用的本地函数,用于异步执行逻辑。给定的函数会从工作线程池中调用,并且可以与主事件循环线程并行执行。
  • [in] complete:当异步逻辑完成或被取消时将调用的本地函数。所给函数将在主事件循环线程中被调用。napi_async_complete_callback 提供了更多详细信息。
  • [in] data:用户提供的数据上下文。这将被传回到 execute 和 complete 函数中。
  • [out] resultnapi_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.】