The docs for AsyncContext
have this example (only slightly modified, but still working) that works synchronously:
#include "napi.h"
void MakeCallbackWithAsyncContext(const Napi::CallbackInfo& info)
{
Napi::Function callback = info[0].As<Napi::Function>();
Napi::Object resource = info.Env().Global().As<Napi::Object>();;
Napi::AsyncContext context(info.Env(), "async_context_test", resource);
callback.MakeCallback(Napi::Object::New(info.Env()),
std::initializer_list<napi_value>{}, context);
}
I'd like to know how to use this asynchronously - here's my latest shot at it:
Napi::AsyncContext* _context;
Napi::FunctionReference* _callback;
Napi::Object* _resource;
void CallBack()
{
_callback->MakeCallback(*_resource, std::initializer_list<napi_value>{}, *_context);
}
void MakeCallbackWithAsyncContext(const Napi::CallbackInfo& info)
{
Napi::Function cb = info[0].As<Napi::Function>();
_callback = new Napi::FunctionReference(Napi::Persistent(cb));
_callback->SuppressDestruct();
_resource = new Napi::ObjectReference(Napi::Persistent(env.Global()));
_resource->SuppressDestruct();
_context = new Napi::AsyncContext(info.Env(), "async_context_test", _resource->Value());
// run CallBack() in different thread
}
The program crashes inside CallBack()
.
It turns out that node-addon-api's AsyncContext
cannot be used on a different thread.
What you can do is use the n-api. Luckily, node-addon-api is only a thin wrapper around n-api, and both can be mixed and matched.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With