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] resource_object:与异步工作相关的对象,会传递给可能的 async_hooks init 钩子。该参数已被弃用,运行时会被忽略。请改用 napi_async_init 中的 async_resource 参数。
  • [in] context:调用回调的异步操作的上下文。这应该是之前从 napi_async_init 获取的值。
  • [out] result:新创建的作用域。

有些情况下(例如,解决 promises)在调用某些 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.】