How to resolve Node.js Promise in N-Api Addon C

Viewed 641

My main problem is to call the asynchronous function from Node.js in addon and get the returned value. I am trying to resolve the promise returned from the called JS function.

index.js

const addon = require('./build/Debug/addon.node');

async function asunc_func() {
    return "Hello asunc_func";
}

const result = addon.test_async(asunc_func); // addon returned a promise
result.then(res => {
    console.log(res);
})

addon.cpp

#include <napi.h>

using namespace Napi;

int do_something_asynchronous(napi_deferred deferred, Napi::Function func, Napi::Env env) {
    napi_value undefined;
    napi_status status;

    status = napi_get_undefined(env, &undefined);
    if (status != napi_ok) return 0;

    napi_value result = func.Call({});

    // I want to get the string "Hello asunc_func" here
    // from called JS function

    napi_valuetype * result_type;
    status = napi_typeof(env, result, result_type); // result_type: napi_object

    if (result) {
      status = napi_resolve_deferred(env, deferred, result);
    } else {
      status = napi_reject_deferred(env, deferred, undefined);
    }
    if (status != napi_ok) return 0;
    deferred = NULL;
}

napi_value test_async(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    Napi::Function func = info[0].As<Napi::Function>();

    napi_deferred deferred;
    napi_value promise;
    napi_status status;

    status = napi_create_promise(env, &deferred, &promise);
    if (status != napi_ok) return NULL;

    do_something_asynchronous(deferred, func, env);
    return promise;
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
  exports.Set(Napi::String::New(env, "test_async"), Napi::Function::New(env, test_async));
  return exports;
}

NODE_API_MODULE(addon, Init)

In addon.cpp I want to call async JS function and get the returned value

I used this doc as example https://nodejs.org/api/n-api.html#n_api_promises

2 Answers

You have to do the same as if you were doing it from JavaScript: call the .then() method of the Promise and register a callback.

Here is the complete example:

Napi::Value ret = asyncFunction.Call({});
if (!ret.IsPromise())
  throw Napi::Error::New(env, "your function did not return a Promise");
Napi::Promise promise = ret.As<Napi::Promise>();

Napi::Value thenValue = promise.Get("then");
if (!thenValue.IsFunction())
  throw Napi::Error::New(env, "Promise is not thenable");
Napi::Function then = thenValue.As<Napi::Function>();

Napi::Function callback = Napi::Function::New(env, Callback, "cpp_callback");
then.Call(promise, {callback});

The Callback function will receive the data from the resolved Promise:

Napi::Value Callback(const Napi::CallbackInfo &info) { 
  printf("Callback called\n");
  printf("Data: %s\n", info[0].ToString().Utf8Value().c_str());
  return info.Env().Null();
}
Related