Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
napi_thread_safe_promise
Advanced tools
Readme
NOTE: This package assumes understanding of N-API
To be used in conjunction with the N-API module to call C++ code from JavaScript.
Allows the user to call thread safe promise functions such (resolve, reject) in the C++ code and return a promise to the javascript code.
npm install napi_thread_safe_promise
binding.gyp
:'include_dirs': ["<!@(node -p \"require('napi_thread_safe_promise').include\")"],
#include "promiseWrapper.h"
Note: Currently latest node version has bugs with regards to gyp so use the latest stable version (should be v14.15.3
as of now)
To have the ability for exceptions add the following to binding.gyp
:
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
'conditions': [
['OS=="win"', { 'defines': [ '_HAS_EXCEPTIONS=1' ] }]
]
Napi::Promise promiseFuncJS(const CallbackInfo& info)
{
// macro takes in info variable as input
return PROMISE(info, {
// now we can write our code here with access to the resolve and reject functions
// we can now take in resolve and reject function to our threaded function because they are thread safe
// note other variables we declared here may not be thread safe (ex. info variable is not thread safe)
std::thread([resolve, reject]() {
// we can pass strings to our resolve and reject functions
resolve("test");
// note secondary calls to resolve or reject will have no effect
resolve("test123");
}).detach();
});
}
Note: if we wish to pass json data to our resolve function we just pass in the json string to the function and parse it in the javascript code
We can use a library like https://github.com/nlohmann/json and use the dump function to accomplish this
Napi::Promise promiseFuncJS(const CallbackInfo& info) {
return promiseFuncWrapper(info.Env(),
[&info](resolveFunc resolve, rejectFunc reject) { // anonymous function passed to thread safe resolve and reject functions
// here we can write our threaded code
std::string arg1 = info[0].As<Napi::String>();
std::thread([resolve, reject, arg1]() {
reject(arg1);
}).detach();
}
);
}
promiseFuncJS(test)
.then(output => {
console.log(output);
})
.catch(err => {
console.error(err);
});
For contribution to this package, create a pull request and the maintainer will look into it.
NAPI-Thread-Safe-Promise is licensed under BSD-3-Clause.
FAQs
C++ napi threadd safe promises
The npm package napi_thread_safe_promise receives a total of 48 weekly downloads. As such, napi_thread_safe_promise popularity was classified as not popular.
We found that napi_thread_safe_promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.