1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
# Promise
Class `Napi::Promise` inherits from class [`Napi::Object`][].
The `Napi::Promise` class, along with its `Napi::Promise::Deferred` class, implement the ability to create, resolve, and reject Promise objects.
The basic approach is to create a `Napi::Promise::Deferred` object and return to your caller the value returned by the `Napi::Promise::Deferred::Promise` method. For example:
```cpp
Napi::Value YourFunction(const Napi::CallbackInfo& info) {
// your code goes here...
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(info.Env());
// deferred needs to survive this call...
return deferred.Promise();
}
```
Later, when the asynchronous process completes, call either the `Resolve` or `Reject` method on the `Napi::Promise::Deferred` object created earlier:
```cpp
deferred.Resolve(String::New(info.Env(), "OK"));
```
## Promise::Deferred Methods
### Factory Method
```cpp
static Napi::Promise::Deferred Napi::Promise::Deferred::New(napi_env env);
```
* `[in] env`: The `napi_env` environment in which to create the `Napi::Promise::Deferred` object.
### Constructor
```cpp
Napi::Promise::Deferred(napi_env env);
```
* `[in] env`: The `napi_env` environment in which to construct the `Napi::Promise::Deferred` object.
### Env
```cpp
Napi::Env Napi::Promise::Deferred::Env() const;
```
Returns the Env environment this `Napi::Promise::Deferred` object is associated with.
### Promise
```cpp
Napi::Promise Napi::Promise::Deferred::Promise() const;
```
Returns the `Napi::Promise` object held by the `Napi::Promise::Deferred` object.
### Resolve
```cpp
void Napi::Promise::Deferred::Resolve(napi_value value) const;
```
Resolves the `Napi::Promise` object held by the `Napi::Promise::Deferred` object.
* `[in] value`: The Node-API primitive value with which to resolve the `Napi::Promise`.
### Reject
```cpp
void Napi::Promise::Deferred::Reject(napi_value value) const;
```
Rejects the Promise object held by the `Napi::Promise::Deferred` object.
* `[in] value`: The Node-API primitive value with which to reject the `Napi::Promise`.
## Promise Methods
### Then
```cpp
Napi::Promise Napi::Promise::Then(napi_value onFulfilled) const;
Napi::Promise Napi::Promise::Then(const Function& onFulfilled) const;
```
Attaches a fulfillment handler to the promise and returns a new promise.
**Parameters:**
* `[in] onFulfilled`: The fulfillment handler for the promise. May be any of:
- `napi_value` – a JavaScript function to be called when the promise is fulfilled.
- `const Function&` – the [`Napi::Function`](function.md) to be called when the promise is fulfilled.
**Returns:** A new `Napi::Promise` that resolves or rejects based on the handler's result.
### Then
```cpp
Napi::Promise Napi::Promise::Then(napi_value onFulfilled, napi_value onRejected) const;
Napi::Promise Napi::Promise::Then(const Function& onFulfilled,
const Function& onRejected) const;
```
Attaches a fulfillment and rejection handlers to the promise and returns a new promise.
**Parameters:**
* `[in] onFulfilled`: The fulfillment handler for the promise. May be any of:
- `napi_value` – a JavaScript function to be called when the promise is fulfilled.
- `const Function&` – the [`Napi::Function`](function.md) to be called when the promise is fulfilled.
* `[in] onRejected` (optional): The rejection handler for the promise. May be any of:
- `napi_value` – a JavaScript function to be called when the promise is rejected.
- `const Function&` – the [`Napi::Function`](function.md) to be called when the promise is rejected.
### Catch
```cpp
Napi::Promise Napi::Promise::Catch(napi_value onRejected) const;
Napi::Promise Napi::Promise::Catch(const Function& onRejected) const;
```
Attaches a rejection handler to the promise and returns a new promise.
**Parameters:**
* `[in] onRejected`: The rejection handler for the promise. May be any of:
- `napi_value` – a JavaScript function to be called when the promise is rejected.
- `const Function&` – the [`Napi::Function`](function.md) to be called when the promise is rejected.
**Returns:** A new `Napi::Promise` that handles rejection cases.
[`Napi::Object`]: ./object.md
[`Napi::Function`]: ./function.md
|