1# Promise
2
3Class `Napi::Promise` inherits from class [`Napi::Object`][].
4
5The `Napi::Promise` class, along with its `Napi::Promise::Deferred` class, implement the ability to create, resolve, and reject Promise objects.
6
7The 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:
8
9```cpp
10Napi::Value YourFunction(const Napi::CallbackInfo& info) {
11  // your code goes here...
12  Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(info.Env());
13  // deferred needs to survive this call...
14  return deferred.Promise();
15}
16```
17
18Later, when the asynchronous process completes, call either the `Resolve` or `Reject` method on the `Napi::Promise::Deferred` object created earlier:
19
20```cpp
21  deferred.Resolve(String::New(info.Env(), "OK"));
22```
23
24## Promise::Deferred Methods
25
26### Factory Method
27
28```cpp
29static Napi::Promise::Deferred Napi::Promise::Deferred::New(napi_env env);
30```
31
32* `[in] env`: The `napi_env` environment in which to create the `Napi::Promise::Deferred` object.
33
34### Constructor
35
36```cpp
37Napi::Promise::Deferred(napi_env env);
38```
39
40* `[in] env`: The `napi_env` environment in which to construct the `Napi::Promise::Deferred` object.
41
42### Env
43
44```cpp
45Napi::Env Napi::Promise::Deferred::Env() const;
46```
47
48Returns the Env environment this `Napi::Promise::Deferred` object is associated with.
49
50### Promise
51
52```cpp
53Napi::Promise Napi::Promise::Deferred::Promise() const;
54```
55
56Returns the `Napi::Promise` object held by the `Napi::Promise::Deferred` object.
57
58### Resolve
59
60```cpp
61void Napi::Promise::Deferred::Resolve(napi_value value) const;
62```
63
64Resolves the `Napi::Promise` object held by the `Napi::Promise::Deferred` object.
65
66* `[in] value`: The Node-API primitive value with which to resolve the `Napi::Promise`.
67
68### Reject
69
70```cpp
71void Napi::Promise::Deferred::Reject(napi_value value) const;
72```
73
74Rejects the Promise object held by the `Napi::Promise::Deferred` object.
75
76* `[in] value`: The Node-API primitive value with which to reject the `Napi::Promise`.
77
78
79[`Napi::Object`]: ./object.md
80