An initializer function is used to ensure that functions are executed on the same instance only once. This topic describes the structure and features of Node.js initializer functions.

Definition

The following code provides an example on how to define a simple initializer function:

exports.my_initializer = function(context, callback) {
    console.log('hello world');
    callback(null, "");
};       

Function name

The function name my_initializer must correspond to the initializer field that you specify when you add the initializer function. For example, if you set the initializer field to main.my_initializer when you create a function, after the initializer function is configured, Function Compute loads the my_initializer function that is defined in the main.js file.

Features

  • Input parameters of an initializer function include the context and callback parameters. These parameters are defined the same as those in event functions. For more information, see context parameter and callback function.
  • The initializer and initializationTimeout fields in the context parameter are designed for initializer functions. If the two fields are not specified, the initializer function is not executed.
    You can use one of the following methods to set the parameters:
  • No value is returned for initializer functions.