Back

[SOLVED] Function invoking other function

  • 0
  • Functions
PfernFSU
1 Mar, 2023, 22:44

I am reading through everything and trying to wrap my head around functions. Is it possible to call/invoke one function from another function? I see the Events, but not sure that is ideal and it smells like a hack. And probably not workable if I need to get a return from the child function in the parent, calling function. I could do a second http call to the child function and handle that way, but using the network layer again seems like overhead. Do you have any documentation or recommended way of handling these scenarios? Thanks in advance.

TL;DR
You can call/invoke one function from another function using the `functions.createExecution()` method. It is also possible to execute a function using the event system triggered by a document or file create/update. However, this may not be ideal if you need to get a return value from the child function in the parent function. Using the network layer again for a second HTTP call can be considered overhead. To avoid duplication of common code, you can create a common folder with code shared across multiple functions.
Drake
1 Mar, 2023, 23:00

it depends on what you're trying to achieve...

Drake
1 Mar, 2023, 23:16

Yes, you can execute a function from a function.

People typically use the event system to execute a function on document or file create/update

PfernFSU
1 Mar, 2023, 23:57

Hmm. Take the notion of push notifications. I need to be able to send them out at numerous times under numerous scenarios - user mentioned in chat, event closed and winner determined, event about to start and players not picked so they aren’t officially entered, etc. There are multiple scenarios. Some can be tied easily to document update/create (like the users name mentioned in chat) but others are not so easily done (players not picked and a day before tournament starts so notification needs sent). My idea was to have a PushNotification function and call it from various other functions (like before record is updated on chat insert, or the function that determines who hasn’t made their picks before a tournament starts). Writing to a temp table/document seems like a hack. While I can do it, not sure I should. Can you point to the documentation that shows how to call a function from another function? And also any best practices around this situation?

Drake
2 Mar, 2023, 00:03

hit send to early >.< hold on

It's all up to you how you want to architect things. In my opinion, having a Push Notification Appwrite Function seems like it would add some overhead.

if you want to reuse code, you could have a common folder with code that is shared across multiple functions.

As for the actual Appwrite Functions, you could have 1 Appwrite Function for each notification and trigger based on event or cron.

Or you could make 1 giant Appwrite Function and then based on the event or whatever, send the appropriate push notification.

Again, it's all up to you how you want to architect things.

Can you point to the documentation that shows how to call a function from another function?

There's nothing special to this. You'd execute a function just like from client: functions.createExecution()

PfernFSU
2 Mar, 2023, 00:14

Ah! Looks like I was overthinking it. The common folder might be perfect. Next time I will search the client documentation too and see if the solution is there. Thanks so much for all your help. Have a great day!

Drake
2 Mar, 2023, 00:17

FYI, the best thing to do to avoid having a duplicate common folder in each one of your functions would be to put the common folder in your functions folder (along side your other functions) and then symlink to that common folder from inside your functions

PfernFSU
2 Mar, 2023, 00:18

Beautiful. Thanks so much! Feel free to close this

Drake
2 Mar, 2023, 00:18

[SOLVED] Function invoking other 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