napi_open_callback_scope


NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env,
                                                 napi_value resource_object,
                                                 napi_async_context context,
                                                 napi_callback_scope* result) 
  • [in] env:调用 API 的环境。

    ¥[in] env: The environment that the API is invoked under.

  • [in] resource_object:与将传递给可能的 async_hooks init 钩子 的异步工作关联的对象。此参数已被弃用,并在运行时被忽略。请改用 napi_async_init 中的 async_resource 参数。

    ¥[in] resource_object: An object associated with the async work that will be passed to possible async_hooks init hooks. This parameter has been deprecated and is ignored at runtime. Use the async_resource parameter in napi_async_init instead.

  • [in] context:调用回调的异步操作的上下文。这应该是之前从 napi_async_init 获得的值。

    ¥[in] context: Context for the async operation that is invoking the callback. This should be a value previously obtained from napi_async_init.

  • [out] result:新创建的范围。

    ¥[out] result: The newly created scope.

在某些情况下(例如,解决 promise),在进行某些 Node-API 调用时,有必要具有与回调关联的等效范围。如果堆栈上没有其他脚本,则可以使用 napi_open_callback_scopenapi_close_callback_scope 函数打开/关闭所需的范围。

¥There are cases (for example, resolving promises) where it is necessary to have the equivalent of the scope associated with a callback in place when making certain Node-API calls. If there is no other script on the stack the napi_open_callback_scope and napi_close_callback_scope functions can be used to open/close the required scope.