1. NAPI_EXTERN napi_status napi_call_function(napi_env env,
    2. napi_value recv,
    3. napi_value func,
    4. size_t argc,
    5. const napi_value* argv,
    6. napi_value* result);
    • [in] env: The environment that the API is invoked under.
    • [in] recv: The this object passed to the called function.
    • [in] func: napi_value representing the JavaScript function to be invoked.
    • [in] argc: The count of elements in the argv array.
    • [in] argv: Array of napi_values representing JavaScript values passed in as arguments to the function.
    • [out] result: napi_value representing the JavaScript object returned.

    Returns napi_ok if the API succeeded.

    This method allows a JavaScript function object to be called from a native add-on. This is the primary mechanism of calling back from the add-on’s native code into JavaScript. For the special case of calling into JavaScript after an async operation, see [napi_make_callback][].

    A sample use case might look as follows. Consider the following JavaScript snippet:

    1. function AddTwo(num) {
    2. return num + 2;
    3. }

    Then, the above function can be invoked from a native add-on using the following code:

    1. // Get the function named "AddTwo" on the global object
    2. napi_value global, add_two, arg;
    3. napi_status status = napi_get_global(env, &global);
    4. if (status != napi_ok) return;
    5. status = napi_get_named_property(env, global, "AddTwo", &add_two);
    6. if (status != napi_ok) return;
    7. // const arg = 1337
    8. status = napi_create_int32(env, 1337, &arg);
    9. if (status != napi_ok) return;
    10. napi_value* argv = &arg;
    11. size_t argc = 1;
    12. // AddTwo(arg);
    13. napi_value return_val;
    14. status = napi_call_function(env, global, add_two, argc, argv, &return_val);
    15. if (status != napi_ok) return;
    16. // Convert the result back to a native type
    17. int32_t result;
    18. status = napi_get_value_int32(env, return_val, &result);
    19. if (status != napi_ok) return;