Calls a specified function.
Description
You can exchange information between processes or functions by calling functions or publishing messages. The difference is that:
Calling functions is bidirectional. After the caller sends a message to the receiver, the caller will receive a message returned by the receiver.
Publishing messages is unidirectional. The sender does not need a response from the receiver.
invokeFunction(params, callback)
Parameter | Type | Description |
params | object | The parameter object. For more information, see the table Parameter descriptions of params. |
callback(err, data) | function | The callback function. The callback function must follow standard JavaScript practices. For more information, see the table Parameter descriptions of callback. |
Parameter | Type | Description |
serviceName | String | The service name. It is the name of the service to which the function belongs in Alibaba Cloud Function Compute. |
functionName | String | The function name that is defined in Alibaba Cloud Function Compute. |
invocationType | String | The call method.
This is a synchronous call by default. |
payload | String|Buffer | The input parameter information of the function. |
Parameter | Type | Description |
err | Error |
|
data | object | The return value of the called function. |
Sample requests
Sample code for calling a function
In the code, the function with serviceName=EdgeFC and functionName=helloworld is called.
'use strict'; const leSdk = require('linkedge-core-sdk'); const fc = new leSdk.FCClient(); module.exports.handler = function (event, context, callback) { var ctx = { custom: { data: 'Custom context from Invoker', }, }; var invokerContext = Buffer.from(JSON.stringify(ctx)).toString('base64'); var invokeParams = { serviceName: 'EdgeFC', functionName: 'helloworld', invocationType: 'Sync', invokerContext: invokerContext, payload: 'String message from Invoker.', }; fc.invokeFunction(invokeParams, (err, data) => { if (err) { console.log(err); } else { console.log(data.payload); // Message from helloworld } callback(null); }); };
Sample code of a function to be called
The following helloworld function code indicates how a called function parses input parameters from the caller and returns the result to the caller.
module.exports.handler = function(event, context, callback) { console.log(event); // String message from Invoker. console.log(context); // { requestId: '4', invokerContext: {custom: { data: 'Custom data from Invoker' }}} console.log(context.invokerContext.custom.data); // Custom data from Invoker console.log('-- hello world'); callback(null, 'Message from helloworld'); // Return the result to Invoker. };