Callback
A validated JS function handle. Accepting napi.Callback as a parameter throws TypeError automatically when the JS caller passes a non-function. Returning a Callback, including as an automatically converted struct field, passes the original JavaScript function through unchanged.
Methods
call(env, args)
args is one of:
- A Zig tuple. Each element is auto-converted to a JS value via type conversion.
- A
[]const napi.Valslice when you have one already built.
The return value is the JS function's return value, as a napi.Val. Convert with .to(env, T).
callWith(env, this, args)
target is the this binding for the call. The args parameter follows the same rules as call.
threadsafe(env, name, T)
Wraps the callback so it can be invoked from any thread. The third argument is the per-call payload type (use void for signal-only callbacks). See ThreadsafeFn and Threadsafe functions guide.
The compatibility API uses Node-API's unbounded queue. Configure real backpressure with the additive options API:
Notes
Callbackis a single-call-time handle. It is valid only for the duration of the call that received it. Wrap it in anapi.Ref(viaenv.createReference) if you want to call it later from the same thread, or in anapi.ThreadsafeFnif you want to call it from another thread.- A non-function passed where
Callbackis expected raisesTypeError: expected function, got <actual type>before your function runs.