Back

[SOLVED] How to execute a function from another function?

  • 1
  • Functions
  • Web
  • Cloud
Ranvir
17 May, 2023, 11:00

Can i fire a custom event from one function, and then listen to it from another one? and pass data from the caller function to the callee function? I'm using JS

TL;DR
To execute a function from another function in JavaScript, you can use the `createExecution` method. Here's an example of how to do it: ```javascript functions.createExecution('[FUNCTION_ID]','Data'); // OR functions.createExecution('[FUNCTION_ID]',JSON.stringify({object:'value'})); ``` You can pass either a string or a JSON object as the data. However, for now, only strings are supported, so if you want to pass an object, you'll need to stringify it. There are a few options for executing the function: 1. Using the server/client side SDK: You can pass any data you
Binyamin
17 May, 2023, 11:27

You have few option to do so:

1. Via server/client side SDK https://appwrite.io/docs/client/functions?sdk=web-default#functionsCreateExecution https://appwrite.io/docs/server/functions?sdk=nodejs-default#functionsCreateExecution In this option you can pass any data you want.

  1. Trigger by event. https://appwrite.io/docs/events#functions-events You can set the event trigger to be the first function. You can see here some example https://discord.com/channels/564160730845151244/1097977347795337318/1097979530943139952 In this option you'll pass only the information that was returned from the first function
Ranvir
17 May, 2023, 11:32

so does createExecution fire the function whose id was passed into it? or does it create a custom execution event i can subscribe to

Binyamin
17 May, 2023, 11:33

createExecution will trigger direct execution of the function. In the data you can pass whatever you want

Binyamin
17 May, 2023, 11:34

When you're listening to function event then your function will execute every time your function do something, and you can check then the data you got from the function

Ranvir
17 May, 2023, 11:38

and syntactically, how would i pass the data to functions.createExecution(<function id>)?

Binyamin
17 May, 2023, 11:39

Like so

TypeScript
functions.createExecution('[FUNCTION_ID]','Data');
// OR
functions.createExecution('[FUNCTION_ID]',JSON.stringify({object:'value'}));

For now you can pass only string that's why you'll need to stringify your object

Binyamin
17 May, 2023, 11:39

And inside your function you can parse the json

Ranvir
17 May, 2023, 11:41

All right that answers my question. thank you!

joeyouss
17 May, 2023, 16:07

[SOLVED] How to execute a function from another function?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more