Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions async_work_promise/node-addon-api/addon.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "napi.h"
#include "worker.h"

Napi::Value DoHeavyMath(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "num1 must be a number")
.ThrowAsJavaScriptException();
return env.Undefined();
}
uint32_t num_1 = info[0].As<Napi::Number>().Uint32Value();

if (!info[1].IsNumber()) {
Napi::TypeError::New(env, "num2 must be a number")
.ThrowAsJavaScriptException();
return env.Undefined();
}
uint32_t num_2 = info[1].As<Napi::Number>().Uint32Value();

DoHeavyMathWorker* worker = new DoHeavyMathWorker(env, num_1, num_2);
worker->Queue();
return worker->GetPromise();
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "doHeavyMath"),
Napi::Function::New(env, DoHeavyMath));
return exports;
}

NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
32 changes: 32 additions & 0 deletions async_work_promise/node-addon-api/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"targets": [{
"target_name": "addon",
"sources": [
"addon.cc"
],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
'defines': [ 'NAPI_CPP_EXCEPTIONS' ],
'conditions': [
[ 'OS=="win"', {
'defines': [ '_HAS_EXCEPTIONS=1' ],
'msvs_settings': {
'VCCLCompilerTool': {
'ExceptionHandling': 1,
},
},
}],
[ 'OS=="linux"', {
"cflags": [ "-fexceptions" ],
"cflags_cc": [ "-fexceptions" ],
}],
[ 'OS=="mac"', {
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
}
}]
],
}]
}
3 changes: 3 additions & 0 deletions async_work_promise/node-addon-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const bindings = require("bindings")("addon");

bindings.doHeavyMath(2, 1).then(console.log);
15 changes: 15 additions & 0 deletions async_work_promise/node-addon-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "async_worker_promise",
"version": "0.0.0",
"description": "AsyncWorker example that returns a Promise",
"main": "index.js",
"scripts": {
"test": "node ."
},
"author": "Matthew Keil (matthewkeil)",
"gypfile": true,
"dependencies": {
"bindings": "^1.5.0",
"node-addon-api": "^7.0.0"
}
}
43 changes: 43 additions & 0 deletions async_work_promise/node-addon-api/worker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "napi.h"

class DoHeavyMathWorker : public Napi::AsyncWorker {
public:
DoHeavyMathWorker(const Napi::Env& env, uint32_t num_1, uint32_t num_2)
: Napi::AsyncWorker{env, "DoHeavyMathWorker"},
m_deferred{env},
m_num_1{num_1},
m_num_2{num_2} {}

/**
* GetPromise associated with _deferred for return to JS
*/
Napi::Promise GetPromise() { return m_deferred.Promise(); }

protected:
/**
* Simulate heavy math work
*/
void Execute() {
if (m_num_2 == 0) {
SetError("Cannot divide by zero");
return;
}
m_result = m_num_1 / m_num_2;
}

/**
* Resolve the promise with the result
*/
void OnOK() { m_deferred.Resolve(Napi::Number::New(Env(), m_result)); }

/**
* Reject the promise with errors
*/
void OnError(const Napi::Error& err) { m_deferred.Reject(err.Value()); }

private:
Napi::Promise::Deferred m_deferred;
uint32_t m_num_1;
uint32_t m_num_2;
uint32_t m_result;
};