When you create an SCF function, you need to specify an execution method. If the Node.js programming language is used, the execution method is similar to index.main_handler, where index indicates that the executed entry file is index.js, and main_handler indicates that the executed entry function is main_handler. When submitting the zip code package by uploading the zip file locally or through COS, please make sure that the root directory of the package contains the specified entry file, the file contains the entry function specified by the definition, and the names of the file and function match those entered in the trigger; otherwise, execution will fail as the entry file or entry function cannot be found.
Input Parameters
The input parameters in the Node.js environment include event, context, and callback, where callback is optional.
event: This parameter is used to pass the trigger event data.
context: This parameter is used to pass runtime information to your handler.
callback (optional): callback is a function that can be used in non-asynchronous handlers to return a response. The response object must be compatible with JSON.stringify. The callback function has two parameters, Error and response. When this function is called, SCF waits for the function to complete before returning the response or error.
Return and Exception
Async handler
Async handlers must use the async keyword, use return to return a response, and use throw to return an error message.
In SCF, if your Node.js function contains an async task, a promise must be returned to ensure that the task is executed on the current invocation. When you fulfill or reject the promise, SCF will return a response or error message.
Note
The promise method does not support returning with the callback method. You should use return.
For non-async handlers, the function will be continuously executed until the function execution completes or times out, and SCF will return a response or error message.
Note
Due to the influence of certain externally imported libraries, the event loop may persistently remain non-empty, causing the function to be unable to return until it times out. To mitigate the impact of external libraries, you can control the timing of the function's return by disabling the event loop wait. By setting context.callbackWaitsForEmptyEventLoop to false, you can modify the default callback behavior to avoid waiting for the event loop to be empty.
You can set context.callbackWaitsForEmptyEventLoop = false; before the callback is executed, allowing the cloud function background to immediately freeze the process after the callback is called, no longer waiting for events in the event loop, and returning immediately after the synchronous process is completed.
The capability to separate synchronous execution returns and asynchronous event handling in functions is supported in the following versions:
Node.js 16.13 (Not Supported)
Node.js 14.18 (Not Supported)
Node.js 12.16 (Supported)
Node.js 10.15 (Supported)
Node.js 8.9 (Not Supported, Soon to be Deprecated)
Node.js 6.10 (Not Supported, Soon to be Deprecated)
After the sync execution process of an entry function is completed and the result is returned, function invocation will immediately return its result, and the return information in the code will be send to the function invoker.
After the sync process is completed and the result returned, the async logic in the code will continue to be executed and processed. The actual function execution process ends and exits only when the async event is completely executed.
Note
SCF logs are collected and processed after the entire execution process ends. Therefore, before the sync execution process is completed and the result is returned, logs and operation information such as time used and memory utilization cannot be provided in the SCF return information. You can query the detailed information in logs by using Request Id after the actual function execution process is completed.
The function execution duration is calculated based on the async event execution duration. If the async event queue cannot get empty or its execution cannot be completed, function timeout will occur. In this case, the invoker may have received the correct response result of the function, but the execution status of the function will still be marked as failure due to timeout, and the timeout period will be calculated as the execution duration.
The synchronous and asynchronous running characteristics, return time, and running duration of Node.js are illustrated in the following diagram:
Async attribute sample
Use the following sample code to create a function, where the setTimeout method is used to set a function that will be executed in 2 seconds:
After saving the code, test the function through the console or call it through the Invoke API. You will see that the function returns in a very short time, with a response time of less than 1 second.
When you view the function execution logs, you can see related statistical information similar to the following:
A 2,005-ms execution period is logged. You can also find in the log that the arg => data is output 2 seconds later, which shows that the relevant async operations are executed in the current invocation after the execution of the sync process is completed, while function invocation ends after execution of the async task is completed.