Skip to content

Commit e4e4c57

Browse files
committed
add promise function
1 parent d2848ed commit e4e4c57

File tree

13 files changed

+434
-124
lines changed

13 files changed

+434
-124
lines changed

API.md

Lines changed: 134 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,188 @@
22

33
### Table of Contents
44

5-
- [HelloObjectAsync](#helloobjectasync)
6-
- [helloAsync](#helloasync)
7-
- [HelloObject](#helloobject)
8-
- [hello](#hello)
9-
- [helloAsync](#helloasync-1)
10-
- [hello](#hello-1)
5+
* [hello][1]
6+
* [Examples][2]
7+
* [helloAsync][3]
8+
* [Parameters][4]
9+
* [Examples][5]
10+
* [helloPromise][6]
11+
* [Parameters][7]
12+
* [Examples][8]
13+
* [HelloObject][9]
14+
* [Examples][10]
15+
* [hello][11]
16+
* [Examples][12]
17+
* [HelloObjectAsync][13]
18+
* [Examples][14]
19+
* [helloAsync][15]
20+
* [Parameters][16]
21+
* [Examples][17]
1122

12-
## HelloObjectAsync
23+
## hello
1324

14-
Asynchronous class, called HelloObjectAsync
25+
This is a synchronous standalone function that logs a string.
1526

16-
**Examples**
27+
### Examples
1728

1829
```javascript
19-
var module = require('index.js');
20-
var Obj = new module.HelloObjectAsync('greg');
30+
const { hello } = require('@mapbox/node-cpp-skel');
31+
const check = hello();
32+
console.log(check); // => "hello world"
2133
```
2234

23-
### helloAsync
35+
Returns **[string][18]**
2436

25-
Say hello while doing expensive work in threads
37+
## helloAsync
2638

27-
**Parameters**
39+
This is an asynchronous standalone function that logs a string.
40+
41+
### Parameters
2842

29-
- `args` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** different ways to alter the string
30-
- `args.louder` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** adds exclamation points to the string
31-
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** from whence the hello comes, returns a string
43+
* `args` **[Object][19]** different ways to alter the string
3244

33-
**Examples**
45+
* `args.louder` **[boolean][20]** adds exclamation points to the string
46+
* `args.buffer` **[boolean][20]** returns value as a node buffer rather than a string
47+
* `callback` **[Function][21]** from whence the hello comes, returns a string
48+
49+
### Examples
3450

3551
```javascript
36-
var module = require('index.js');
37-
var Obj = new module.HelloObjectAsync('greg');
38-
Obj.helloAsync({ louder: true }, function(err, result) {
52+
const { helloAsync } = require('@mapbox/node-cpp-skel');
53+
helloAsync({ louder: true }, function(err, result) {
3954
if (err) throw err;
40-
console.log(result); // => '...threads are busy async bees...hello greg!!!'
55+
console.log(result); // => "...threads are busy async bees...hello
56+
world!!!!"
4157
});
4258
```
4359
44-
Returns **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
60+
Returns **[string][18]**
61+
62+
## helloPromise
63+
64+
This is a function that returns a promise. It multiplies a string N times.
65+
66+
### Parameters
67+
68+
* `options` **[Object][19]?** different ways to alter the string
69+
70+
* `options.phrase` **[string][18]** the string to multiply (optional, default `hello`)
71+
* `options.multiply` **[Number][22]** duplicate the string this number of times (optional, default `1`)
72+
73+
### Examples
74+
75+
```javascript
76+
const { helloPromise } = require('@mapbox/node-cpp-skel');
77+
const result = await helloAsync({ phrase: 'Howdy', multiply: 3 });
78+
console.log(result); // HowdyHowdyHowdy
79+
```
80+
81+
Returns **[Promise][23]**
4582
4683
## HelloObject
4784
4885
Synchronous class, called HelloObject
4986
50-
**Examples**
87+
### Examples
5188
5289
```javascript
53-
var module = require('index.js');
54-
var Obj = new module.HelloObject('greg');
90+
const { HelloObject } = require('@mapbox/node-cpp-skel');
91+
const Obj = new HelloObject('greg');
5592
```
5693
5794
### hello
5895
5996
Say hello
6097
61-
**Examples**
98+
#### Examples
6299
63100
```javascript
64-
var x = Obj.hello();
101+
const x = Obj.hello();
65102
console.log(x); // => '...initialized an object...hello greg'
66103
```
67104
68-
Returns **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
105+
Returns **[String][18]**
69106
70-
## helloAsync
107+
## HelloObjectAsync
71108
72-
This is an asynchronous standalone function that logs a string.
109+
Asynchronous class, called HelloObjectAsync
110+
111+
### Examples
112+
113+
```javascript
114+
const { HelloObjectAsync } = require('@mapbox/node-cpp-skel');
115+
const Obj = new module.HelloObjectAsync('greg');
116+
```
73117
74-
**Parameters**
118+
### helloAsync
119+
120+
Say hello while doing expensive work in threads
121+
122+
#### Parameters
75123
76-
- `args` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** different ways to alter the string
77-
- `args.louder` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** adds exclamation points to the string
78-
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** from whence the hello comes, returns a string
124+
* `args` **[Object][19]** different ways to alter the string
79125
80-
**Examples**
126+
* `args.louder` **[boolean][20]** adds exclamation points to the string
127+
* `args.buffer` **[buffer][24]** returns object as a node buffer rather then string
128+
* `callback` **[Function][21]** from whence the hello comes, returns a string
129+
130+
#### Examples
81131
82132
```javascript
83-
var module = require('./path/to/lib/index.js');
84-
module.helloAsync({ louder: true }, function(err, result) {
133+
const { HelloObjectAsync } = require('@mapbox/node-cpp-skel');
134+
const Obj = new HelloObjectAsync('greg');
135+
Obj.helloAsync({ louder: true }, function(err, result) {
85136
if (err) throw err;
86-
console.log(result); // => "...threads are busy async bees...hello world!!!!"
137+
console.log(result); // => '...threads are busy async bees...hello greg!!!'
87138
});
88139
```
89140
90-
Returns **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
141+
Returns **[String][18]**
91142
92-
## hello
143+
[1]: #hello
93144
94-
This is a synchronous standalone function that logs a string.
145+
[2]: #examples
95146
96-
**Examples**
147+
[3]: #helloasync
97148
98-
```javascript
99-
var module = require('./path/to/lib/index.js');
100-
var check = module.hello();
101-
console.log(check); // => "hello world"
102-
```
149+
[4]: #parameters
150+
151+
[5]: #examples-1
152+
153+
[6]: #hellopromise
154+
155+
[7]: #parameters-1
156+
157+
[8]: #examples-2
158+
159+
[9]: #helloobject
160+
161+
[10]: #examples-3
162+
163+
[11]: #hello-1
164+
165+
[12]: #examples-4
166+
167+
[13]: #helloobjectasync
168+
169+
[14]: #examples-5
170+
171+
[15]: #helloasync-1
172+
173+
[16]: #parameters-2
174+
175+
[17]: #examples-6
176+
177+
[18]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
178+
179+
[19]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
180+
181+
[20]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
182+
183+
[21]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
184+
185+
[22]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
186+
187+
[23]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
103188
104-
Returns **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
189+
[24]: https://nodejs.org/api/buffer.html

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 2/21/2022
2+
3+
* Add `helloPromise` function example using [Napi::Promise](https://github.com/nodejs/node-addon-api/blob/c54aeef5fd37d3304e61af189672f9f61d403e6f/doc/promises.md)
4+
* Move all JSDoc to lib/index.js since versions >=5 do not support the `--polyglot` flag and give docs a little refresh
5+
16
# 1/9/2018
27

38
* Add memory stats option to bench tests
@@ -14,7 +19,7 @@
1419

1520
* Add [sanitzer flag doc](https://github.com/mapbox/node-cpp-skel/pull/84)
1621
* Add [sanitizer script](hhttps://github.com/mapbox/node-cpp-skel/pull/85) and enable [leak sanitizer](https://github.com/mapbox/node-cpp-skel/commit/725601e4c7df6cb8477a128f018fb064a9f6f9aa)
17-
*
22+
*
1823

1924
# 10/20/2017
2025

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
'./src/module.cpp',
7878
'./src/standalone/hello.cpp',
7979
'./src/standalone_async/hello_async.cpp',
80+
'./src/standalone_promise/hello_promise.cpp',
8081
'./src/object_sync/hello.cpp',
8182
'./src/object_async/hello_async.cpp'
8283
],

lib/index.js

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,102 @@
11
"use strict";
22

3-
module.exports = require('./binding/module.node');
3+
const {
4+
hello,
5+
helloAsync,
6+
helloPromise,
7+
Hello,
8+
HelloAsync
9+
} = require('./binding/module.node');
10+
11+
module.exports = {
12+
/**
13+
* This is a synchronous standalone function that logs a string.
14+
* @name hello
15+
* @returns {string}
16+
* @example
17+
* const { hello } = require('@mapbox/node-cpp-skel');
18+
* const check = hello();
19+
* console.log(check); // => "hello world"
20+
*/
21+
hello,
22+
23+
/**
24+
* This is an asynchronous standalone function that logs a string.
25+
* @name helloAsync
26+
* @param {Object} args - different ways to alter the string
27+
* @param {boolean} args.louder - adds exclamation points to the string
28+
* @param {boolean} args.buffer - returns value as a node buffer rather than a string
29+
* @param {Function} callback - from whence the hello comes, returns a string
30+
* @returns {string}
31+
* @example
32+
* const { helloAsync } = require('@mapbox/node-cpp-skel');
33+
* helloAsync({ louder: true }, function(err, result) {
34+
* if (err) throw err;
35+
* console.log(result); // => "...threads are busy async bees...hello
36+
* world!!!!"
37+
* });
38+
*/
39+
helloAsync,
40+
41+
/**
42+
* This is a function that returns a promise. It multiplies a string N times.
43+
* @name helloPromise
44+
* @param {Object} [options] - different ways to alter the string
45+
* @param {string} [options.phrase=hello] - the string to multiply
46+
* @param {Number} [options.multiply=1] - duplicate the string this number of times
47+
* @returns {Promise}
48+
* @example
49+
* const { helloPromise } = require('@mapbox/node-cpp-skel');
50+
* const result = await helloAsync({ phrase: 'Howdy', multiply: 3 });
51+
* console.log(result); // HowdyHowdyHowdy
52+
*/
53+
helloPromise,
54+
55+
/**
56+
* Synchronous class, called HelloObject
57+
* @class HelloObject
58+
* @example
59+
* const { HelloObject } = require('@mapbox/node-cpp-skel');
60+
* const Obj = new HelloObject('greg');
61+
*/
62+
63+
/**
64+
* Say hello
65+
*
66+
* @name hello
67+
* @memberof HelloObject
68+
* @returns {String}
69+
* @example
70+
* const x = Obj.hello();
71+
* console.log(x); // => '...initialized an object...hello greg'
72+
*/
73+
HelloObject,
74+
75+
/**
76+
* Asynchronous class, called HelloObjectAsync
77+
* @class HelloObjectAsync
78+
* @example
79+
* const { HelloObjectAsync } = require('@mapbox/node-cpp-skel');
80+
* const Obj = new module.HelloObjectAsync('greg');
81+
*/
82+
83+
/**
84+
* Say hello while doing expensive work in threads
85+
*
86+
* @name helloAsync
87+
* @memberof HelloObjectAsync
88+
* @param {Object} args - different ways to alter the string
89+
* @param {boolean} args.louder - adds exclamation points to the string
90+
* @param {buffer} args.buffer - returns object as a node buffer rather then string
91+
* @param {Function} callback - from whence the hello comes, returns a string
92+
* @returns {String}
93+
* @example
94+
* const { HelloObjectAsync } = require('@mapbox/node-cpp-skel');
95+
* const Obj = new HelloObjectAsync('greg');
96+
* Obj.helloAsync({ louder: true }, function(err, result) {
97+
* if (err) throw err;
98+
* console.log(result); // => '...threads are busy async bees...hello greg!!!'
99+
* });
100+
*/
101+
HelloObjectAsync
102+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"scripts": {
1212
"test": "tape test/*.test.js",
1313
"install": "node-pre-gyp install --fallback-to-build",
14-
"docs": "documentation build src/standalone_async/*.cpp src/standalone/*.cpp src/object_sync/*.cpp src/object_async/*.cpp --polyglot -f md -o API.md"
14+
"docs": "documentation build ./lib/index.js -f md > API.md"
1515
},
1616
"author": "Mapbox",
1717
"license": "ISC",

src/module.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "object_sync/hello.hpp"
33
#include "standalone/hello.hpp"
44
#include "standalone_async/hello_async.hpp"
5+
#include "standalone_promise/hello_promise.hpp"
56
#include <napi.h>
67
// #include "your_code.hpp"
78

@@ -13,6 +14,9 @@ Napi::Object init(Napi::Env env, Napi::Object exports)
1314
// expose helloAsync method
1415
exports.Set(Napi::String::New(env, "helloAsync"), Napi::Function::New(env, standalone_async::helloAsync));
1516

17+
// expose helloPromise method
18+
exports.Set(Napi::String::New(env, "helloPromise"), Napi::Function::New(env, standalone_promise::helloPromise));
19+
1620
// expose HelloObject class
1721
object_sync::HelloObject::Init(env, exports);
1822

0 commit comments

Comments
 (0)