Skip to main content
JSCallback wraps TypeScript functions so they can be used as C function pointers.

Basic Usage

Closure Capture

Thread Safety

Set threadsafe: true for callbacks that may be invoked from a different thread:

Thread-Safe C Function Pointers

When threadsafe: true, cb.ptr returns a real C function pointer (trampoline address) that can be passed directly to native C code:
Internally, napi_create_threadsafe_function creates a TSFN, while 16 pre-compiled native trampoline functions are allocated — one per callback slot. cb.ptr returns the trampoline address via getCallbackPtr. When native C code calls this address, TrampolineDispatcher synchronously invokes the TypeScript callback and returns the result.

Cleanup

Always call close() to release resources:

Multiple Callbacks

Passing to Native Functions