3

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 Answers2

1

Answer to the following stack overflow post is explaining this scenario too. In that answer the resolve/reject of the deferred promise is being done in CompleteMyPromise1() function.

How to create async function using NAPI that return Promises

Satyan
  • 1,346
  • 8
  • 15
  • 1
    The way you are calling `do_something_asynchronous()` is not really an async call, it is a blocking call just like any other C function call. – Satyan Aug 07 '20 at 06:50
  • Thank you, information about event-loop blocking will help me. But I needed something different. In your example you return Promise from Addon to Node.js and Node.js app resolve it. I need the opposite - Node.js passes Promise to Addon and Addon resolve it. Do you know how to do it?) – Anna Kiriakidi Aug 09 '20 at 18:44
0

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();
}
mmomtchev
  • 2,497
  • 1
  • 8
  • 23