Release Notes
Announcements
exports.main_handler = async (event, context) => {console.log(event);console.log(context);return event};
exports.main_handler = (event,context,callback)=>{console.log(event);console.log(context);callback(null,"hello world");}
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.event, context, and callback, where callback is optional.callback is a function that can be used in non-async handlers to return a response. The response object must be compatible with JSON.stringify. The callback function has two parameters: Error and response. When invoking this function, SCF will wait for the function to complete before returning a response or error.async keyword, use return to return a response, and use throw to return an error message.callback method. You should use return.exports.main_handler = async(event,context,callback) => {const promise = new Promise((resolve,reject) => {setTimeout(function() {resolve('success')// reject('failure')}, 2000)})return promise};
context.callbackWaitsForEmptyEventLoop to false to avoid waiting for the event loop to get empty.
By setting context.callbackWaitsForEmptyEventLoop = false; before the callback callback is executed, the SCF backend can freeze the process immediately after the callback callback is invoked and return immediately after the sync process is completed without waiting for the event in the event loop.exports.main_handler = (event, context,callback) => {context.callbackWaitsForEmptyEventLoop = falsecallback(null,'success')setTimeout(() => {console.log('finish')},5000);};
Request Id after the actual function execution process is completed.
setTimeout method is used to set a function that will be executed in 2 seconds:'use strict';exports.main_handler = (event, context, callback) => {console.log("Hello World")console.log(event)setTimeout(timeoutfunc, 2000, 'data');callback(null, event);};function timeoutfunc(arg) {console.log(`arg => ${arg}`);}
Invoke API. You can see that the function can return the result in a response period below 1 second.
You can see the following statistics in the function execution log:START RequestId: 1d71ddf8-5022-4461-84b7-e3a152403ffcEvent RequestId: 1d71ddf8-5022-4461-84b7-e3a152403ffc2020-03-18T09:16:13.440Z 1d71ddf8-5022-4461-84b7-e3a152403ffc Hello World2020-03-18T09:16:13.440Z 1d71ddf8-5022-4461-84b7-e3a152403ffc { key1: 'test value 1', key2: 'test value 2' }2020-03-18T09:16:15.443Z 1d71ddf8-5022-4461-84b7-e3a152403ffc arg => dataEND RequestId: 1d71ddf8-5022-4461-84b7-e3a152403ffcReport RequestId: 1d71ddf8-5022-4461-84b7-e3a152403ffc Duration:2005ms Memory:128MB MemUsage:13.425781MB
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.Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan