18c339a94Sopenharmony_ci# Function 28c339a94Sopenharmony_ci 38c339a94Sopenharmony_ciThe `Napi::Function` class provides a set of methods for creating a function object in 48c339a94Sopenharmony_cinative code that can later be called from JavaScript. The created function is not 58c339a94Sopenharmony_ciautomatically visible from JavaScript. Instead it needs to be part of the add-on's 68c339a94Sopenharmony_cimodule exports or be returned by one of the module's exported functions. 78c339a94Sopenharmony_ci 88c339a94Sopenharmony_ciIn addition the `Napi::Function` class also provides methods that can be used to call 98c339a94Sopenharmony_cifunctions that were created in JavaScript and passed to the native add-on. 108c339a94Sopenharmony_ci 118c339a94Sopenharmony_ciThe `Napi::Function` class inherits its behavior from the `Napi::Object` class (for more info 128c339a94Sopenharmony_cisee: [`Napi::Object`](object.md)). 138c339a94Sopenharmony_ci 148c339a94Sopenharmony_ci> For callbacks that will be called with asynchronous events from a 158c339a94Sopenharmony_ci> non-JavaScript thread, please refer to [`Napi::ThreadSafeFunction`][] for more 168c339a94Sopenharmony_ci> examples. 178c339a94Sopenharmony_ci 188c339a94Sopenharmony_ci## Example 198c339a94Sopenharmony_ci 208c339a94Sopenharmony_ci```cpp 218c339a94Sopenharmony_ci#include <napi.h> 228c339a94Sopenharmony_ci 238c339a94Sopenharmony_ciusing namespace Napi; 248c339a94Sopenharmony_ci 258c339a94Sopenharmony_ciValue Fn(const CallbackInfo& info) { 268c339a94Sopenharmony_ci Env env = info.Env(); 278c339a94Sopenharmony_ci // ... 288c339a94Sopenharmony_ci return String::New(env, "Hello World"); 298c339a94Sopenharmony_ci} 308c339a94Sopenharmony_ci 318c339a94Sopenharmony_ciObject Init(Env env, Object exports) { 328c339a94Sopenharmony_ci exports.Set(String::New(env, "fn"), Function::New<Fn>(env)); 338c339a94Sopenharmony_ci return exports; 348c339a94Sopenharmony_ci} 358c339a94Sopenharmony_ci 368c339a94Sopenharmony_ciNODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) 378c339a94Sopenharmony_ci``` 388c339a94Sopenharmony_ci 398c339a94Sopenharmony_ciThe above code can be used from JavaScript as follows: 408c339a94Sopenharmony_ci 418c339a94Sopenharmony_ci```js 428c339a94Sopenharmony_ciconst addon = require('./addon'); 438c339a94Sopenharmony_ciaddon.fn(); 448c339a94Sopenharmony_ci``` 458c339a94Sopenharmony_ci 468c339a94Sopenharmony_ciWith the `Napi::Function` class it is possible to call a JavaScript function object 478c339a94Sopenharmony_cifrom a native add-on with two different methods: `Call` and `MakeCallback`. 488c339a94Sopenharmony_ciThe API of these two methods is very similar, but they are used in different 498c339a94Sopenharmony_cicontexts. The `MakeCallback` method is used to call from native code back into 508c339a94Sopenharmony_ciJavaScript after returning from an [asynchronous operation](async_operations.md) 518c339a94Sopenharmony_ciand in general in situations which don't have an existing JavaScript function on 528c339a94Sopenharmony_cithe stack. The `Call` method is used when there is already a JavaScript function 538c339a94Sopenharmony_cion the stack (for example when running a native method called from JavaScript). 548c339a94Sopenharmony_ci 558c339a94Sopenharmony_ci## Type definitions 568c339a94Sopenharmony_ci 578c339a94Sopenharmony_ci### Napi::Function::VoidCallback 588c339a94Sopenharmony_ci 598c339a94Sopenharmony_ciThis is the type describing a callback returning `void` that will be invoked 608c339a94Sopenharmony_cifrom JavaScript. 618c339a94Sopenharmony_ci 628c339a94Sopenharmony_ci```cpp 638c339a94Sopenharmony_ciusing VoidCallback = void (*)(const Napi::CallbackInfo& info); 648c339a94Sopenharmony_ci``` 658c339a94Sopenharmony_ci 668c339a94Sopenharmony_ci### Napi::Function::Callback 678c339a94Sopenharmony_ci 688c339a94Sopenharmony_ciThis is the type describing a callback returning a value that will be invoked 698c339a94Sopenharmony_cifrom JavaScript. 708c339a94Sopenharmony_ci 718c339a94Sopenharmony_ci 728c339a94Sopenharmony_ci```cpp 738c339a94Sopenharmony_ciusing Callback = Value (*)(const Napi::CallbackInfo& info); 748c339a94Sopenharmony_ci``` 758c339a94Sopenharmony_ci 768c339a94Sopenharmony_ci## Methods 778c339a94Sopenharmony_ci 788c339a94Sopenharmony_ci### Constructor 798c339a94Sopenharmony_ci 808c339a94Sopenharmony_ciCreates a new empty instance of `Napi::Function`. 818c339a94Sopenharmony_ci 828c339a94Sopenharmony_ci```cpp 838c339a94Sopenharmony_ciNapi::Function::Function(); 848c339a94Sopenharmony_ci``` 858c339a94Sopenharmony_ci 868c339a94Sopenharmony_ci### Constructor 878c339a94Sopenharmony_ci 888c339a94Sopenharmony_ciCreates a new instance of the `Napi::Function` object. 898c339a94Sopenharmony_ci 908c339a94Sopenharmony_ci```cpp 918c339a94Sopenharmony_ciNapi::Function::Function(napi_env env, napi_value value); 928c339a94Sopenharmony_ci``` 938c339a94Sopenharmony_ci 948c339a94Sopenharmony_ci- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. 958c339a94Sopenharmony_ci- `[in] value`: The `napi_value` which is a handle for a JavaScript function. 968c339a94Sopenharmony_ci 978c339a94Sopenharmony_ciReturns a non-empty `Napi::Function` instance. 988c339a94Sopenharmony_ci 998c339a94Sopenharmony_ci### New 1008c339a94Sopenharmony_ci 1018c339a94Sopenharmony_ciCreates an instance of a `Napi::Function` object. 1028c339a94Sopenharmony_ci 1038c339a94Sopenharmony_ci```cpp 1048c339a94Sopenharmony_citemplate <Napi::VoidCallback cb> 1058c339a94Sopenharmony_cistatic Napi::Function New(napi_env env, 1068c339a94Sopenharmony_ci const char* utf8name = nullptr, 1078c339a94Sopenharmony_ci void* data = nullptr); 1088c339a94Sopenharmony_ci``` 1098c339a94Sopenharmony_ci 1108c339a94Sopenharmony_ci- `[template] cb`: The native function to invoke when the JavaScript function is 1118c339a94Sopenharmony_ciinvoked. 1128c339a94Sopenharmony_ci- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. 1138c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string to be used as the name of the function. 1148c339a94Sopenharmony_ci- `[in] data`: User-provided data context. This will be passed back into the 1158c339a94Sopenharmony_cifunction when invoked later. 1168c339a94Sopenharmony_ci 1178c339a94Sopenharmony_ciReturns an instance of a `Napi::Function` object. 1188c339a94Sopenharmony_ci 1198c339a94Sopenharmony_ci### New 1208c339a94Sopenharmony_ci 1218c339a94Sopenharmony_ciCreates an instance of a `Napi::Function` object. 1228c339a94Sopenharmony_ci 1238c339a94Sopenharmony_ci```cpp 1248c339a94Sopenharmony_citemplate <Napi::Callback cb> 1258c339a94Sopenharmony_cistatic Napi::Function New(napi_env env, 1268c339a94Sopenharmony_ci const char* utf8name = nullptr, 1278c339a94Sopenharmony_ci void* data = nullptr); 1288c339a94Sopenharmony_ci``` 1298c339a94Sopenharmony_ci 1308c339a94Sopenharmony_ci- `[template] cb`: The native function to invoke when the JavaScript function is 1318c339a94Sopenharmony_ciinvoked. 1328c339a94Sopenharmony_ci- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. 1338c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string to be used as the name of the function. 1348c339a94Sopenharmony_ci- `[in] data`: User-provided data context. This will be passed back into the 1358c339a94Sopenharmony_cifunction when invoked later. 1368c339a94Sopenharmony_ci 1378c339a94Sopenharmony_ciReturns an instance of a `Napi::Function` object. 1388c339a94Sopenharmony_ci 1398c339a94Sopenharmony_ci### New 1408c339a94Sopenharmony_ci 1418c339a94Sopenharmony_ciCreates an instance of a `Napi::Function` object. 1428c339a94Sopenharmony_ci 1438c339a94Sopenharmony_ci```cpp 1448c339a94Sopenharmony_citemplate <Napi::VoidCallback cb> 1458c339a94Sopenharmony_cistatic Napi::Function New(napi_env env, 1468c339a94Sopenharmony_ci const std::string& utf8name, 1478c339a94Sopenharmony_ci void* data = nullptr); 1488c339a94Sopenharmony_ci``` 1498c339a94Sopenharmony_ci 1508c339a94Sopenharmony_ci- `[template] cb`: The native function to invoke when the JavaScript function is 1518c339a94Sopenharmony_ciinvoked. 1528c339a94Sopenharmony_ci- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. 1538c339a94Sopenharmony_ci- `[in] utf8name`: String to be used as the name of the function. 1548c339a94Sopenharmony_ci- `[in] data`: User-provided data context. This will be passed back into the 1558c339a94Sopenharmony_cifunction when invoked later. 1568c339a94Sopenharmony_ci 1578c339a94Sopenharmony_ciReturns an instance of a `Napi::Function` object. 1588c339a94Sopenharmony_ci 1598c339a94Sopenharmony_ci### New 1608c339a94Sopenharmony_ci 1618c339a94Sopenharmony_ciCreates an instance of a `Napi::Function` object. 1628c339a94Sopenharmony_ci 1638c339a94Sopenharmony_ci```cpp 1648c339a94Sopenharmony_citemplate <Napi::Callback cb> 1658c339a94Sopenharmony_cistatic Napi::Function New(napi_env env, 1668c339a94Sopenharmony_ci const std::string& utf8name, 1678c339a94Sopenharmony_ci void* data = nullptr); 1688c339a94Sopenharmony_ci``` 1698c339a94Sopenharmony_ci 1708c339a94Sopenharmony_ci- `[template] cb`: The native function to invoke when the JavaScript function is 1718c339a94Sopenharmony_ciinvoked. 1728c339a94Sopenharmony_ci- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. 1738c339a94Sopenharmony_ci- `[in] utf8name`: String to be used as the name of the function. 1748c339a94Sopenharmony_ci- `[in] data`: User-provided data context. This will be passed back into the 1758c339a94Sopenharmony_cifunction when invoked later. 1768c339a94Sopenharmony_ci 1778c339a94Sopenharmony_ciReturns an instance of a `Napi::Function` object. 1788c339a94Sopenharmony_ci 1798c339a94Sopenharmony_ci### New 1808c339a94Sopenharmony_ci 1818c339a94Sopenharmony_ciCreates an instance of a `Napi::Function` object. 1828c339a94Sopenharmony_ci 1838c339a94Sopenharmony_ci```cpp 1848c339a94Sopenharmony_citemplate <typename Callable> 1858c339a94Sopenharmony_cistatic Napi::Function Napi::Function::New(napi_env env, Callable cb, const char* utf8name = nullptr, void* data = nullptr); 1868c339a94Sopenharmony_ci``` 1878c339a94Sopenharmony_ci 1888c339a94Sopenharmony_ci- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. 1898c339a94Sopenharmony_ci- `[in] cb`: Object that implements `Callable`. 1908c339a94Sopenharmony_ci- `[in] utf8name`: Null-terminated string to be used as the name of the function. 1918c339a94Sopenharmony_ci- `[in] data`: User-provided data context. This will be passed back into the 1928c339a94Sopenharmony_cifunction when invoked later. 1938c339a94Sopenharmony_ci 1948c339a94Sopenharmony_ciReturns an instance of a `Napi::Function` object. 1958c339a94Sopenharmony_ci 1968c339a94Sopenharmony_ci### New 1978c339a94Sopenharmony_ci 1988c339a94Sopenharmony_ci```cpp 1998c339a94Sopenharmony_citemplate <typename Callable> 2008c339a94Sopenharmony_cistatic Napi::Function Napi::Function::New(napi_env env, Callable cb, const std::string& utf8name, void* data = nullptr); 2018c339a94Sopenharmony_ci``` 2028c339a94Sopenharmony_ci 2038c339a94Sopenharmony_ci- `[in] env`: The `napi_env` environment in which to construct the `Napi::Function` object. 2048c339a94Sopenharmony_ci- `[in] cb`: Object that implements `Callable`. 2058c339a94Sopenharmony_ci- `[in] utf8name`: String to be used as the name of the function. 2068c339a94Sopenharmony_ci- `[in] data`: User-provided data context. This will be passed back into the 2078c339a94Sopenharmony_cifunction when invoked later. 2088c339a94Sopenharmony_ci 2098c339a94Sopenharmony_ciReturns an instance of a `Napi::Function` object. 2108c339a94Sopenharmony_ci 2118c339a94Sopenharmony_ci### New 2128c339a94Sopenharmony_ci 2138c339a94Sopenharmony_ciCreates a new JavaScript value from one that represents the constructor for the 2148c339a94Sopenharmony_ciobject. 2158c339a94Sopenharmony_ci 2168c339a94Sopenharmony_ci```cpp 2178c339a94Sopenharmony_ciNapi::Object Napi::Function::New(const std::initializer_list<napi_value>& args) const; 2188c339a94Sopenharmony_ci``` 2198c339a94Sopenharmony_ci 2208c339a94Sopenharmony_ci- `[in] args`: Initializer list of JavaScript values as `napi_value` representing 2218c339a94Sopenharmony_cithe arguments of the constructor function. 2228c339a94Sopenharmony_ci 2238c339a94Sopenharmony_ciReturns a new JavaScript object. 2248c339a94Sopenharmony_ci 2258c339a94Sopenharmony_ci### New 2268c339a94Sopenharmony_ci 2278c339a94Sopenharmony_ciCreates a new JavaScript value from one that represents the constructor for the 2288c339a94Sopenharmony_ciobject. 2298c339a94Sopenharmony_ci 2308c339a94Sopenharmony_ci```cpp 2318c339a94Sopenharmony_ciNapi::Object Napi::Function::New(const std::vector<napi_value>& args) const; 2328c339a94Sopenharmony_ci``` 2338c339a94Sopenharmony_ci 2348c339a94Sopenharmony_ci- `[in] args`: Vector of JavaScript values as `napi_value` representing the 2358c339a94Sopenharmony_ciarguments of the constructor function. 2368c339a94Sopenharmony_ci 2378c339a94Sopenharmony_ciReturns a new JavaScript object. 2388c339a94Sopenharmony_ci 2398c339a94Sopenharmony_ci### New 2408c339a94Sopenharmony_ci 2418c339a94Sopenharmony_ciCreates a new JavaScript value from one that represents the constructor for the 2428c339a94Sopenharmony_ciobject. 2438c339a94Sopenharmony_ci 2448c339a94Sopenharmony_ci```cpp 2458c339a94Sopenharmony_ciNapi::Object Napi::Function::New(size_t argc, const napi_value* args) const; 2468c339a94Sopenharmony_ci``` 2478c339a94Sopenharmony_ci 2488c339a94Sopenharmony_ci- `[in] argc`: The number of the arguments passed to the constructor function. 2498c339a94Sopenharmony_ci- `[in] args`: Array of JavaScript values as `napi_value` representing the 2508c339a94Sopenharmony_ciarguments of the constructor function. 2518c339a94Sopenharmony_ci 2528c339a94Sopenharmony_ciReturns a new JavaScript object. 2538c339a94Sopenharmony_ci 2548c339a94Sopenharmony_ci### Call 2558c339a94Sopenharmony_ci 2568c339a94Sopenharmony_ciCalls a Javascript function from a native add-on. 2578c339a94Sopenharmony_ci 2588c339a94Sopenharmony_ci```cpp 2598c339a94Sopenharmony_ciNapi::Value Napi::Function::Call(const std::initializer_list<napi_value>& args) const; 2608c339a94Sopenharmony_ci``` 2618c339a94Sopenharmony_ci 2628c339a94Sopenharmony_ci- `[in] args`: Initializer list of JavaScript values as `napi_value` representing 2638c339a94Sopenharmony_cithe arguments of the function. 2648c339a94Sopenharmony_ci 2658c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 2668c339a94Sopenharmony_ci 2678c339a94Sopenharmony_ci### Call 2688c339a94Sopenharmony_ci 2698c339a94Sopenharmony_ciCalls a JavaScript function from a native add-on. 2708c339a94Sopenharmony_ci 2718c339a94Sopenharmony_ci```cpp 2728c339a94Sopenharmony_ciNapi::Value Napi::Function::Call(const std::vector<napi_value>& args) const; 2738c339a94Sopenharmony_ci``` 2748c339a94Sopenharmony_ci 2758c339a94Sopenharmony_ci- `[in] args`: Vector of JavaScript values as `napi_value` representing the 2768c339a94Sopenharmony_ciarguments of the function. 2778c339a94Sopenharmony_ci 2788c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 2798c339a94Sopenharmony_ci 2808c339a94Sopenharmony_ci### Call 2818c339a94Sopenharmony_ci 2828c339a94Sopenharmony_ciCalls a Javascript function from a native add-on. 2838c339a94Sopenharmony_ci 2848c339a94Sopenharmony_ci```cpp 2858c339a94Sopenharmony_ciNapi::Value Napi::Function::Call(size_t argc, const napi_value* args) const; 2868c339a94Sopenharmony_ci``` 2878c339a94Sopenharmony_ci 2888c339a94Sopenharmony_ci- `[in] argc`: The number of the arguments passed to the function. 2898c339a94Sopenharmony_ci- `[in] args`: Array of JavaScript values as `napi_value` representing the 2908c339a94Sopenharmony_ciarguments of the function. 2918c339a94Sopenharmony_ci 2928c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 2938c339a94Sopenharmony_ci 2948c339a94Sopenharmony_ci### Call 2958c339a94Sopenharmony_ci 2968c339a94Sopenharmony_ciCalls a Javascript function from a native add-on. 2978c339a94Sopenharmony_ci 2988c339a94Sopenharmony_ci```cpp 2998c339a94Sopenharmony_ciNapi::Value Napi::Function::Call(napi_value recv, const std::initializer_list<napi_value>& args) const; 3008c339a94Sopenharmony_ci``` 3018c339a94Sopenharmony_ci 3028c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the called function. 3038c339a94Sopenharmony_ci- `[in] args`: Initializer list of JavaScript values as `napi_value` representing 3048c339a94Sopenharmony_cithe arguments of the function. 3058c339a94Sopenharmony_ci 3068c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 3078c339a94Sopenharmony_ci 3088c339a94Sopenharmony_ci### Call 3098c339a94Sopenharmony_ci 3108c339a94Sopenharmony_ciCalls a Javascript function from a native add-on. 3118c339a94Sopenharmony_ci 3128c339a94Sopenharmony_ci```cpp 3138c339a94Sopenharmony_ciNapi::Value Napi::Function::Call(napi_value recv, const std::vector<napi_value>& args) const; 3148c339a94Sopenharmony_ci``` 3158c339a94Sopenharmony_ci 3168c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the called function. 3178c339a94Sopenharmony_ci- `[in] args`: Vector of JavaScript values as `napi_value` representing the 3188c339a94Sopenharmony_ciarguments of the function. 3198c339a94Sopenharmony_ci 3208c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 3218c339a94Sopenharmony_ci 3228c339a94Sopenharmony_ci### Call 3238c339a94Sopenharmony_ci 3248c339a94Sopenharmony_ciCalls a Javascript function from a native add-on. 3258c339a94Sopenharmony_ci 3268c339a94Sopenharmony_ci```cpp 3278c339a94Sopenharmony_ciNapi::Value Napi::Function::Call(napi_value recv, size_t argc, const napi_value* args) const; 3288c339a94Sopenharmony_ci``` 3298c339a94Sopenharmony_ci 3308c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the called function. 3318c339a94Sopenharmony_ci- `[in] argc`: The number of the arguments passed to the function. 3328c339a94Sopenharmony_ci- `[in] args`: Array of JavaScript values as `napi_value` representing the 3338c339a94Sopenharmony_ciarguments of the function. 3348c339a94Sopenharmony_ci 3358c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 3368c339a94Sopenharmony_ci 3378c339a94Sopenharmony_ci### MakeCallback 3388c339a94Sopenharmony_ci 3398c339a94Sopenharmony_ciCalls a Javascript function from a native add-on after an asynchronous operation. 3408c339a94Sopenharmony_ci 3418c339a94Sopenharmony_ci```cpp 3428c339a94Sopenharmony_ciNapi::Value Napi::Function::MakeCallback(napi_value recv, const std::initializer_list<napi_value>& args, napi_async_context context = nullptr) const; 3438c339a94Sopenharmony_ci``` 3448c339a94Sopenharmony_ci 3458c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the called function. 3468c339a94Sopenharmony_ci- `[in] args`: Initializer list of JavaScript values as `napi_value` representing 3478c339a94Sopenharmony_cithe arguments of the function. 3488c339a94Sopenharmony_ci- `[in] context`: Context for the async operation that is invoking the callback. 3498c339a94Sopenharmony_ciThis should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). 3508c339a94Sopenharmony_ciHowever `nullptr` is also allowed, which indicates the current async context 3518c339a94Sopenharmony_ci(if any) is to be used for the callback. 3528c339a94Sopenharmony_ci 3538c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 3548c339a94Sopenharmony_ci 3558c339a94Sopenharmony_ci### MakeCallback 3568c339a94Sopenharmony_ci 3578c339a94Sopenharmony_ciCalls a Javascript function from a native add-on after an asynchronous operation. 3588c339a94Sopenharmony_ci 3598c339a94Sopenharmony_ci```cpp 3608c339a94Sopenharmony_ciNapi::Value Napi::Function::MakeCallback(napi_value recv, const std::vector<napi_value>& args, napi_async_context context = nullptr) const; 3618c339a94Sopenharmony_ci``` 3628c339a94Sopenharmony_ci 3638c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the called function. 3648c339a94Sopenharmony_ci- `[in] args`: List of JavaScript values as `napi_value` representing the 3658c339a94Sopenharmony_ciarguments of the function. 3668c339a94Sopenharmony_ci- `[in] context`: Context for the async operation that is invoking the callback. 3678c339a94Sopenharmony_ciThis should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). 3688c339a94Sopenharmony_ciHowever `nullptr` is also allowed, which indicates the current async context 3698c339a94Sopenharmony_ci(if any) is to be used for the callback. 3708c339a94Sopenharmony_ci 3718c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 3728c339a94Sopenharmony_ci 3738c339a94Sopenharmony_ci### MakeCallback 3748c339a94Sopenharmony_ci 3758c339a94Sopenharmony_ciCalls a Javascript function from a native add-on after an asynchronous operation. 3768c339a94Sopenharmony_ci 3778c339a94Sopenharmony_ci```cpp 3788c339a94Sopenharmony_ciNapi::Value Napi::Function::MakeCallback(napi_value recv, size_t argc, const napi_value* args, napi_async_context context = nullptr) const; 3798c339a94Sopenharmony_ci``` 3808c339a94Sopenharmony_ci 3818c339a94Sopenharmony_ci- `[in] recv`: The `this` object passed to the called function. 3828c339a94Sopenharmony_ci- `[in] argc`: The number of the arguments passed to the function. 3838c339a94Sopenharmony_ci- `[in] args`: Array of JavaScript values as `napi_value` representing the 3848c339a94Sopenharmony_ciarguments of the function. 3858c339a94Sopenharmony_ci- `[in] context`: Context for the async operation that is invoking the callback. 3868c339a94Sopenharmony_ciThis should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). 3878c339a94Sopenharmony_ciHowever `nullptr` is also allowed, which indicates the current async context 3888c339a94Sopenharmony_ci(if any) is to be used for the callback. 3898c339a94Sopenharmony_ci 3908c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 3918c339a94Sopenharmony_ci 3928c339a94Sopenharmony_ci## Operator 3938c339a94Sopenharmony_ci 3948c339a94Sopenharmony_ci```cpp 3958c339a94Sopenharmony_ciNapi::Value Napi::Function::operator ()(const std::initializer_list<napi_value>& args) const; 3968c339a94Sopenharmony_ci``` 3978c339a94Sopenharmony_ci 3988c339a94Sopenharmony_ci- `[in] args`: Initializer list of JavaScript values as `napi_value`. 3998c339a94Sopenharmony_ci 4008c339a94Sopenharmony_ciReturns a `Napi::Value` representing the JavaScript value returned by the function. 4018c339a94Sopenharmony_ci 4028c339a94Sopenharmony_ci[`Napi::ThreadSafeFunction`]: ./threadsafe_function.md 403