Skip to content

Commit b0de26f

Browse files
authored
feat: add node-addon-api example for async_work_promise (#212)
* feat: add node-addon-api example for async_work_promise
1 parent ebcb9fe commit b0de26f

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "napi.h"
2+
#include "worker.h"
3+
4+
Napi::Value DoHeavyMath(const Napi::CallbackInfo& info) {
5+
Napi::Env env = info.Env();
6+
7+
if (!info[0].IsNumber()) {
8+
Napi::TypeError::New(env, "num1 must be a number")
9+
.ThrowAsJavaScriptException();
10+
return env.Undefined();
11+
}
12+
uint32_t num_1 = info[0].As<Napi::Number>().Uint32Value();
13+
14+
if (!info[1].IsNumber()) {
15+
Napi::TypeError::New(env, "num2 must be a number")
16+
.ThrowAsJavaScriptException();
17+
return env.Undefined();
18+
}
19+
uint32_t num_2 = info[1].As<Napi::Number>().Uint32Value();
20+
21+
DoHeavyMathWorker* worker = new DoHeavyMathWorker(env, num_1, num_2);
22+
worker->Queue();
23+
return worker->GetPromise();
24+
}
25+
26+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
27+
exports.Set(Napi::String::New(env, "doHeavyMath"),
28+
Napi::Function::New(env, DoHeavyMath));
29+
return exports;
30+
}
31+
32+
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"targets": [{
3+
"target_name": "addon",
4+
"sources": [
5+
"addon.cc"
6+
],
7+
"include_dirs": [
8+
"<!@(node -p \"require('node-addon-api').include\")"
9+
],
10+
'defines': [ 'NAPI_CPP_EXCEPTIONS' ],
11+
'conditions': [
12+
[ 'OS=="win"', {
13+
'defines': [ '_HAS_EXCEPTIONS=1' ],
14+
'msvs_settings': {
15+
'VCCLCompilerTool': {
16+
'ExceptionHandling': 1,
17+
},
18+
},
19+
}],
20+
[ 'OS=="linux"', {
21+
"cflags": [ "-fexceptions" ],
22+
"cflags_cc": [ "-fexceptions" ],
23+
}],
24+
[ 'OS=="mac"', {
25+
'xcode_settings': {
26+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
27+
'CLANG_CXX_LIBRARY': 'libc++',
28+
}
29+
}]
30+
],
31+
}]
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const bindings = require("bindings")("addon");
2+
3+
bindings.doHeavyMath(2, 1).then(console.log);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "async_worker_promise",
3+
"version": "0.0.0",
4+
"description": "AsyncWorker example that returns a Promise",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node ."
8+
},
9+
"author": "Matthew Keil (matthewkeil)",
10+
"gypfile": true,
11+
"dependencies": {
12+
"bindings": "^1.5.0",
13+
"node-addon-api": "^7.0.0"
14+
}
15+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "napi.h"
2+
3+
class DoHeavyMathWorker : public Napi::AsyncWorker {
4+
public:
5+
DoHeavyMathWorker(const Napi::Env& env, uint32_t num_1, uint32_t num_2)
6+
: Napi::AsyncWorker{env, "DoHeavyMathWorker"},
7+
m_deferred{env},
8+
m_num_1{num_1},
9+
m_num_2{num_2} {}
10+
11+
/**
12+
* GetPromise associated with _deferred for return to JS
13+
*/
14+
Napi::Promise GetPromise() { return m_deferred.Promise(); }
15+
16+
protected:
17+
/**
18+
* Simulate heavy math work
19+
*/
20+
void Execute() {
21+
if (m_num_2 == 0) {
22+
SetError("Cannot divide by zero");
23+
return;
24+
}
25+
m_result = m_num_1 / m_num_2;
26+
}
27+
28+
/**
29+
* Resolve the promise with the result
30+
*/
31+
void OnOK() { m_deferred.Resolve(Napi::Number::New(Env(), m_result)); }
32+
33+
/**
34+
* Reject the promise with errors
35+
*/
36+
void OnError(const Napi::Error& err) { m_deferred.Reject(err.Value()); }
37+
38+
private:
39+
Napi::Promise::Deferred m_deferred;
40+
uint32_t m_num_1;
41+
uint32_t m_num_2;
42+
uint32_t m_result;
43+
};

0 commit comments

Comments
 (0)