I'm learning how to use node-addon-api and now I'm stuck on async/await management. I cannot understand how to handle the case where a native function receive an object which has an async function.
Javascript:
const addon = require('bindings')('addon');
class Foo {
async doAsync() {
...
}
}
const useFoo = () => {
const foo = new Foo();
await addon.doStuff(foo);
}
Native:
#include <napi.h>
using namespace Napi;
Napi::Object doStuff(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
MyObject* foo = Napi::ObjectWrap<MyObject>::Unwrap(info[0].As<Napi::Object>());
// How should I await for this?
foo.doAsync();
...
}
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "doStuff"), Napi::Function::New(env, doStuff));
return exports;
}
NODE_API_MODULE(addon, InitAll)
My problem is that I do not find any documentation about this behavior. I read about how to create a promise in native but not how to use it when received from JS.