Skip to content

Commit 9d38f61

Browse files
jschlightmhdawson
authored andcommitted
doc: New Promise and Reference docs
PR-URL: #243 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
1 parent 43ff9fa commit 9d38f61

File tree

2 files changed

+181
-6
lines changed

2 files changed

+181
-6
lines changed

doc/promises.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
1-
# Promise
2-
3-
You are reading a draft of the next documentation and it's in continuous update so
1+
You are reading a draft of the next documentation and it's in continuos update so
42
if you don't find what you need please refer to:
53
[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/)
4+
5+
# Promise
6+
7+
The Promise class, along with its Promise::Deferred class, implement the ability to create, resolve, and reject Promise objects.
8+
9+
The basic approach is to create a Promise::Deferred object and return to your caller the value returned by the Promise::Deferred::Promise method. For example:
10+
11+
```cpp
12+
Value YourFunction(const CallbackInfo& info) {
13+
// your code goes here...
14+
Promise::Deferred deferred = Promise::Deferred::New(info.Env());
15+
// deferred needs to survive this call...
16+
return deferred.Promise();
17+
}
18+
```
19+
20+
Later, when the asynchronous process completes, call either the `Resolve` or `Reject` method on the Promise::Deferred object created earlier:
21+
22+
```cpp
23+
deferred.Resolve(String::New(info.Env(), "OK"));
24+
```
25+
26+
## Promise::Deferred Methods
27+
28+
### Factory Method
29+
30+
```cpp
31+
static Promise::Deferred Promise::Deferred::New(napi_env env);
32+
```
33+
34+
* `[in] env`: The `napi_env` environment in which to create the Deferred object.
35+
36+
### Constructor
37+
38+
```cpp
39+
Promise::Deferred(napi_env env);
40+
```
41+
42+
* `[in] env`: The `napi_env` environment in which to construct the Deferred object.
43+
44+
### Promise
45+
46+
```cpp
47+
Promise Promise::Deferred::Promise() const;
48+
```
49+
50+
Returns the Promise object held by the Promise::Deferred object.
51+
52+
### Resolve
53+
54+
```cpp
55+
void Promise::Deferred::Resolve(napi_value value) const;
56+
```
57+
58+
Resolves the Promise object held by the Promise::Deferred object.
59+
60+
* `[in] value`: The N-API primitive value with which to resolve the Promise.
61+
62+
### Reject
63+
64+
```cpp
65+
void Promise::Deferred::Reject(napi_value value) const;
66+
```
67+
68+
Rejects the Promise object held by the Promise::Deferred object.
69+
70+
* `[in] value`: The N-API primitive value with which to reject the Promise.

doc/reference.md

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,115 @@
1-
# Reference
2-
3-
You are reading a draft of the next documentation and it's in continuous update so
1+
You are reading a draft of the next documentation and it's in continuos update so
42
if you don't find what you need please refer to:
53
[C++ wrapper classes for the ABI-stable C APIs for Node.js](https://nodejs.github.io/node-addon-api/)
4+
5+
# Reference (template)
6+
7+
Holds a counted reference to a [Value](value.md) object; initially a weak reference unless otherwise specified, may be changed to/from a strong reference by adjusting the refcount.
8+
9+
The referenced Value is not immediately destroyed when the reference count is zero; it is merely then eligible for garbage-collection if there are no other references to the Value.
10+
11+
Reference objects allocated in static space, such as a global static instance, must call the `SuppressDestruct` method to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid.
12+
13+
The following classes inherit, either directly or indirectly, from Reference:
14+
15+
* [ObjectWrap](object_wrap.md)
16+
* [ObjectReference](object_reference.md)
17+
* [FunctionReference](function_reference.md)
18+
19+
## Methods
20+
21+
### Factory Method
22+
23+
```cpp
24+
static Reference<T> New(const T& value, uint32_t initialRefcount = 0);
25+
```
26+
27+
* `[in] value`: The value which is to be referenced.
28+
29+
* `[in] initialRefcount`: The initial reference count.
30+
31+
### Empty Constructor
32+
33+
```cpp
34+
Reference();
35+
```
36+
37+
Creates a new _empty_ Reference instance.
38+
39+
### Constructor
40+
41+
```cpp
42+
Reference(napi_env env, napi_value value);
43+
```
44+
45+
* `[in] env`: The `napi_env` environment in which to construct the Reference object.
46+
47+
* `[in] value`: The N-API primitive value to be held by the Reference.
48+
49+
### Env
50+
51+
```cpp
52+
Napi::Env Env() const;
53+
```
54+
55+
Returns the `Env` value in which the Reference was instantiated.
56+
57+
### IsEmpty
58+
59+
```cpp
60+
bool IsEmpty() const;
61+
```
62+
63+
Determines whether the value held by the Reference is empty.
64+
65+
### Value
66+
67+
```cpp
68+
T Value() const;
69+
```
70+
71+
Returns the value held by the Reference.
72+
73+
### Ref
74+
75+
```cpp
76+
uint32_t Ref();
77+
```
78+
79+
Increments the reference count for the Reference and returns the resulting reference count. Throws an error if the increment fails.
80+
81+
### Unref
82+
83+
```cpp
84+
uint32_t Unref();
85+
```
86+
87+
Decrements the reference count for the Reference and returns the resulting reference count. Throws an error if the decrement fails.
88+
89+
### Reset (Empty)
90+
91+
```cpp
92+
void Reset();
93+
```
94+
95+
Sets the value held by the Reference to be empty.
96+
97+
### Reset
98+
99+
```cpp
100+
void Reset(const T& value, uint32_t refcount = 0);
101+
```
102+
103+
* `[in] value`: The value which is to be referenced.
104+
105+
* `[in] initialRefcount`: The initial reference count.
106+
107+
Sets the value held by the Reference.
108+
109+
### SuppressDestruct
110+
111+
```cpp
112+
void SuppressDestruct();
113+
```
114+
115+
Call this method on a Reference that is declared as static data to prevent its destructor, running at program shutdown time, from attempting to reset the reference when the environment is no longer valid.

0 commit comments

Comments
 (0)