@@ -42,6 +42,7 @@ The documentation for N-API is structured as follows:
4242* [ Working with JavaScript Functions] [ ]
4343* [ Object Wrap] [ ]
4444* [ Asynchronous Operations] [ ]
45+ * [ Promises] [ ]
4546
4647The N-API is a C API that ensures ABI stability across Node.js versions
4748and different compiler levels. However, we also understand that a C++
@@ -3395,6 +3396,142 @@ support it:
33953396
33963397<!-- it's very convenient to have all the anchors indexed -->
33973398<!--lint disable no-unused-definitions remark-lint-->
3399+ ## Promises
3400+
3401+ N-API provides facilities for creating `Promise` objects as described in
3402+ [Section 25.4][] of the ECMA specification. It implements promises as a pair of
3403+ objects. When a promise is created by `napi_create_promise()`, a "deferred"
3404+ object is created and returned alongside the `Promise`. The deferred object is
3405+ bound to the created `Promise` and is the only means to resolve or reject the
3406+ `Promise` using `napi_resolve_deferred()` or `napi_reject_deferred()`. The
3407+ deferred object that is created by `napi_create_promise()` is freed by
3408+ `napi_resolve_deferred()` or `napi_reject_deferred()`. The `Promise` object may
3409+ be returned to JavaScript where it can be used in the usual fashion.
3410+
3411+ For example, to create a promise and pass it to an asynchronous worker:
3412+ ```c
3413+ napi_deferred deferred;
3414+ napi_value promise;
3415+ napi_status status;
3416+
3417+ // Create the promise.
3418+ status = napi_create_promise(env, &deferred, &promise);
3419+ if (status != napi_ok) return NULL;
3420+
3421+ // Pass the deferred to a function that performs an asynchronous action.
3422+ do_something_asynchronous(deferred);
3423+
3424+ // Return the promise to JS
3425+ return promise;
3426+ ```
3427+
3428+ The above function ` do_something_asynchronous() ` would perform its asynchronous
3429+ action and then it would resolve or reject the deferred, thereby concluding the
3430+ promise and freeing the deferred:
3431+ ``` c
3432+ napi_deferred deferred;
3433+ napi_value undefined;
3434+ napi_status status;
3435+
3436+ // Create a value with which to conclude the deferred.
3437+ status = napi_get_undefined(env, &undefined);
3438+ if (status != napi_ok) return NULL ;
3439+
3440+ // Resolve or reject the promise associated with the deferred depending on
3441+ // whether the asynchronous action succeeded.
3442+ if (asynchronous_action_succeeded) {
3443+ status = napi_resolve_deferred(env, deferred, undefined);
3444+ } else {
3445+ status = napi_reject_deferred(env, deferred, undefined);
3446+ }
3447+ if (status != napi_ok) return NULL ;
3448+
3449+ // At this point the deferred has been freed, so we should assign NULL to it.
3450+ deferred = NULL ;
3451+ ```
3452+
3453+ ### napi_create_promise
3454+ <!-- YAML
3455+ added: REPLACEME
3456+ -->
3457+ ``` C
3458+ NAPI_EXTERN napi_status napi_create_promise (napi_env env,
3459+ napi_deferred* deferred,
3460+ napi_value* promise);
3461+ ```
3462+
3463+ - `[in] env`: The environment that the API is invoked under.
3464+ - `[out] deferred`: A newly created deferred object which can later be passed to
3465+ `napi_resolve_deferred()` or `napi_reject_deferred()` to resolve resp. reject
3466+ the associated promise.
3467+ - `[out] promise`: The JavaScript promise associated with the deferred object.
3468+
3469+ Returns `napi_ok` if the API succeeded.
3470+
3471+ This API creates a deferred object and a JavaScript promise.
3472+
3473+ ### napi_resolve_deferred
3474+ <!-- YAML
3475+ added: REPLACEME
3476+ -->
3477+ ```C
3478+ NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env,
3479+ napi_deferred deferred,
3480+ napi_value resolution);
3481+ ```
3482+
3483+ - ` [in] env ` : The environment that the API is invoked under.
3484+ - ` [in] deferred ` : The deferred object whose associated promise to resolve.
3485+ - ` [in] resolution ` : The value with which to resolve the promise.
3486+
3487+ This API resolves a JavaScript promise by way of the deferred object
3488+ with which it is associated. Thus, it can only be used to resolve JavaScript
3489+ promises for which the corresponding deferred object is available. This
3490+ effectively means that the promise must have been created using
3491+ ` napi_create_promise() ` and the deferred object returned from that call must
3492+ have been retained in order to be passed to this API.
3493+
3494+ The deferred object is freed upon successful completion.
3495+
3496+ ### napi_reject_deferred
3497+ <!-- YAML
3498+ added: REPLACEME
3499+ -->
3500+ ``` C
3501+ NAPI_EXTERN napi_status napi_reject_deferred (napi_env env,
3502+ napi_deferred deferred,
3503+ napi_value rejection);
3504+ ```
3505+
3506+ - `[in] env`: The environment that the API is invoked under.
3507+ - `[in] deferred`: The deferred object whose associated promise to resolve.
3508+ - `[in] rejection`: The value with which to reject the promise.
3509+
3510+ This API rejects a JavaScript promise by way of the deferred object
3511+ with which it is associated. Thus, it can only be used to reject JavaScript
3512+ promises for which the corresponding deferred object is available. This
3513+ effectively means that the promise must have been created using
3514+ `napi_create_promise()` and the deferred object returned from that call must
3515+ have been retained in order to be passed to this API.
3516+
3517+ The deferred object is freed upon successful completion.
3518+
3519+ ### napi_is_promise
3520+ <!-- YAML
3521+ added: REPLACEME
3522+ -->
3523+ ```C
3524+ NAPI_EXTERN napi_status napi_is_promise(napi_env env,
3525+ napi_value promise,
3526+ bool* is_promise);
3527+ ```
3528+
3529+ - ` [in] env ` : The environment that the API is invoked under.
3530+ - ` [in] promise ` : The promise to examine
3531+ - ` [out] is_promise ` : Flag indicating whether ` promise ` is a native promise
3532+ object - that is, a promise object created by the underlying engine.
3533+
3534+ [ Promises ] : #n_api_promises
33983535[ Asynchronous Operations ] : #n_api_asynchronous_operations
33993536[ Basic N-API Data Types ] : #n_api_basic_n_api_data_types
34003537[ ECMAScript Language Specification ] : https://tc39.github.io/ecma262/
@@ -3406,6 +3543,7 @@ support it:
34063543[ Section 9.1.6 ] : https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
34073544[ Section 12.5.5 ] : https://tc39.github.io/ecma262/#sec-typeof-operator
34083545[ Section 24.3 ] : https://tc39.github.io/ecma262/#sec-dataview-objects
3546+ [ Section 25.4 ] : https://tc39.github.io/ecma262/#sec-promise-objects
34093547[ Working with JavaScript Functions ] : #n_api_working_with_javascript_functions
34103548[ Working with JavaScript Properties ] : #n_api_working_with_javascript_properties
34113549[ Working with JavaScript Values ] : #n_api_working_with_javascript_values
0 commit comments