node.jsnode.js-addonn-apinode-addon-api

How to use node-addon-api's AsyncContext asynchronously


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().


Solution

  • 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.