Functions

Stitch functions can be used to execute server-side application logic. They are written in JavaScript ES6+ and don't require server.

Here are some key limitations of functions:

  • They stop execution once they return
  • They can run for up to 60 seconds, using up to 256 MB of memory
  • They cannot import modules or use some of the core JavaScript functionality, such as global object types, mathematical, number, string, array, and object APIs

Stitch functions can be imported via the CLI or from Stitch UI. For a simple function that we have named multiply, we could add this code in the UI:

exports = function(a, b) {
return a * b;
};

And then we could call it from another function, webhook, or trigger within Stitch:

context.functions.execute("multiply", a, b);

We can also trigger its execution within a Stitch JSON expression using %function:

{
"%%true": {
"%function": {
"name": "multiply",
"arguments": [3,4]
}
}
}

We can even call this function from our client application using Stitch SDK (JavaScript, Android, or macOS):

const client = Stitch.defaultAppClient;
client.callFunction("multiply", [3, 4]).then(result => {
console.log(result) // Output: 12
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset