
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
napi-threadsafe-deferred
Advanced tools
A thread-safe variant of a Napi-Promise which can be resolved from any thread.
A thread-safe variant of a Napi-Promise which can be resolved from any thread.
In case you stumble across the issue, that you tried to resolve or reject a promise from a thread different from the main javascript thread, this module is for you. It provides you the class ThreadSafeDeferred which is a replacement class for Napi::Promise::Deferred. It uses Napi::ThreadSafeFunction's release callback (which is called from the main thread) to resolve or reject the promise from there.
npm i napi-threadsafe-deferred
binding.gyp ...
"include_dirs": [
...
"<!@(node -p \"require('napi-threadsafe-deferred').include\")"
...
],
"dependencies": [
...
"<!(node -p \"require('napi-threadsafe-deferred').gyp\")"
...
]
...
ThreadSafeDeferred.hpp.Napi::Value myFunctionBoundToJS(const Napi::CallbackInfo & info) {
auto myDeferred = new ThreadSafeDeferred(Env());
// -> store a pointer or reference to the deferred somewhere
return myDeferred->Promise();
}
Resolve or Reject from any thread you like.You can resolve a promise without a value:
void functionCalledFromAnotherThread() {
...
myDeferred.Reslove();
}
...with a value:
void functionCalledFromAnotherThread() {
...
myDeferred.Reslove(THREADSAFE_DEFERRED_RESOLVER(Napi::Number::New(env, 42)));
}
Resolving with a value is done by passing a lambda call back which will be called from the main thread during promise resolution. THREADSAFE_DEFERRED_RESOLVER makes the symbols available as copy ([=])
You can manually pass a lambda, if you need more control over the symbols or if you want to use the callback to release memory:
void functionCalledFromAnotherThread() {
...
myDeferred.Reslove([&something]{
delete something;
return Napi::Number::New(env, 42);
});
}
You can reject a promise using the Reject function without parameter or with a string, which is used as reason for the exception on which the rejection is based:
void functionCalledFromAnotherThread() {
...
myDeferred.Reject("Gime gime gime - you tried to invoke man after midnight.");
...
}
As the resolution of the ThreadSafeDeferred is handled asynchronously - the object will - in case it resides on heap - delete itself after the underlying Napi::Promise::Deferred has been resolved. As the object is still needed after your call to Resolve or Reject you must not delete it yourself.
FAQs
A thread-safe variant of a Napi-Promise which can be resolved from any thread.
We found that napi-threadsafe-deferred 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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.