Promises
For Promises that do not need a background thread, build them directly. Useful for adapting an existing async source (a Zig event loop, a callback-based API, a synchronous result you want to defer) into a JS Promise.
createPromise returns a Promise struct with two fields:
promiseis thenapi.Valyou return from your function.deferredis the handle you use to settle the promise.
Resolving and rejecting
Deferred has two methods, both single-use:
After either is called, the deferred handle is consumed. Calling either method again is undefined behavior.
To reject with a JS Error value:
env.createError builds a JS Error without throwing it.
Using a callback to settle later
The deferred handle can be stashed somewhere and resolved later, for example from a callback or a different function call:
A Deferred is just a handle. It does not by itself keep the JS event loop alive. If your only references to the promise are inside Zig and JS has dropped the original, the resolved value will be visible to no one. Make sure JS is still holding the promise (or you are using a callback chain that does).
When to use this vs runWorker
runWorker: there is real work to do on a background thread. The result of that work resolves the promise.createPromise: there is no extra thread needed. You just want to return a promise that will be settled later, possibly from another N-API call.