napi_make_callback
NAPI_EXTERN napi_status napi_make_callback(napi_env env,
napi_async_context async_context,
napi_value recv,
napi_value func,
size_t argc,
const napi_value* argv,
napi_value* result);
-
[in] env
:调用 API 的环境。¥
[in] env
: The environment that the API is invoked under. -
[in] async_context
:调用回调的异步操作的上下文。这通常应该是以前从napi_async_init
获得的值。为了保持与以前版本的 ABI 兼容性,为async_context
传递NULL
不会导致错误。但是,这会导致异步钩子的错误操作。潜在问题包括使用AsyncLocalStorage
API 时丢失异步上下文。¥
[in] async_context
: Context for the async operation that is invoking the callback. This should normally be a value previously obtained fromnapi_async_init
. In order to retain ABI compatibility with previous versions, passingNULL
forasync_context
does not result in an error. However, this results in incorrect operation of async hooks. Potential issues include loss of async context when using theAsyncLocalStorage
API. -
[in] recv
:传递给被调用函数的this
值。¥
[in] recv
: Thethis
value passed to the called function. -
[in] func
:napi_value
表示要调用的 JavaScript 函数。¥
[in] func
:napi_value
representing the JavaScript function to be invoked. -
[in] argc
:argv
数组中的元素数。¥
[in] argc
: The count of elements in theargv
array. -
[in] argv
:JavaScript 值数组napi_value
表示函数的参数。如果argc
为零,则可以通过传入NULL
来省略此参数。¥
[in] argv
: Array of JavaScript values asnapi_value
representing the arguments to the function. Ifargc
is zero this parameter may be omitted by passing inNULL
. -
[out] result
:napi_value
表示返回的 JavaScript 对象。¥
[out] result
:napi_value
representing the JavaScript object returned.
如果 API 成功,则返回 napi_ok
。
¥Returns napi_ok
if the API succeeded.
此方法允许从原生附加组件调用 JavaScript 函数对象。这个 API 类似于 napi_call_function
。但是,它用于在从异步操作返回后(当堆栈上没有其他脚本时)从原生代码调用回 JavaScript。它是 node::MakeCallback
的一个相当简单的封装器。
¥This method allows a JavaScript function object to be called from a native
add-on. This API is similar to napi_call_function
. However, it is used to call
from native code back into JavaScript after returning from an async
operation (when there is no other script on the stack). It is a fairly simple
wrapper around node::MakeCallback
.
请注意,不必在 napi_async_complete_callback
中使用 napi_make_callback
;在这种情况下,回调的异步上下文已经设置好,因此直接调用 napi_call_function
就足够且合适了。实现不使用 napi_create_async_work
的自定义异步行为时,可能需要使用 napi_make_callback
函数。
¥Note it is not necessary to use napi_make_callback
from within a
napi_async_complete_callback
; in that situation the callback's async
context has already been set up, so a direct call to napi_call_function
is sufficient and appropriate. Use of the napi_make_callback
function
may be required when implementing custom async behavior that does not use
napi_create_async_work
.
在回调期间由 JavaScript 在微任务队列上安排的任何 process.nextTick
或 Promises 在返回到 C/C++ 之前运行。
¥Any process.nextTick
s or Promises scheduled on the microtask queue by
JavaScript during the callback are ran before returning back to C/C++.