How to instantiate and return a wrapped C++ object from AsyncWorker

297 views Asked by At

I'm following the Node Addon API documentation to resolve a promise on the C++ side. On resolve I'd like to return a C++ object back to JS. My code is similar to what is here. The main difference is that I'd like to return an instance of the object below, which is Napi::ObjectWrap'ed:

class MyWrappedObject final : public Napi::ObjectWrap<MyWrappedObject> {
public:
  static Napi::Object Init(Napi::Env env, Napi::Object exports) {} ;
  MyWrappedObject(const Napi::CallbackInfo &info) : Napi::ObjectWrap<MyWrappedObject>(info) {};
}

i.e. in the async worker I'd like to resolve like so:

void OnOK() {
  const auto wrapped_object = MyWrappedObject();
  deferred.Resolve(Napi::Object::New(Env(), wrapped_object));
}

the issue here is that the ctor for Napi::ObjectWrap requires a Napi::CallbackInfo as argument but the context during the OnOK call doesn't provide that callback info. So I'm wondering what am I missing here, I've read the documents but cannot find any examples where they return a wrapped c++ object from a promise.

0

There are 0 answers