Any trigger can be used to perform lengthy operations and return before that operation has finished. This really depends on whether the code you run in the trigger is synchronous or asynchronous.
If you take another look at the whole paragraph in the docs, it says:
You can use an afterSave handler to perform lengthy operations after sending a response back to the client. In order to respond to the client before the afterSave handler completes, your handler may not return a promise and your afterSave handler may not use async/await.
The example in the docs above that paragraph is asynchronous and will return immediately because it does not await query.get. It can be synchronous if you instead write await query.get, in that case the trigger will not complete until all chained calls have finished. The same is true for class and file triggers.
For further reading, I suggest to search for async/await JavaScript promises and synchronous / asynchronous JavaScript execution, which is a deep topic that really never gets old
Thank you so much for the detailed response. I didn’t realise that using async in afterSave would delay the .save callback, I figured that would only happen in beforeSave. You’re absolutely correct.
I will chime in here since i’m the one who added the file hooks (I’m glad you like them!).
The way the afterSaveFile hook is written it allows you to have both options (wait for the task to finish or not wait). If you want to do a long running task and not wait you would do something like this:
Parse.Cloud.afterSaveFile(async () => {
// Lets wait for an object to be fetched
const object = new Parse.User();
object.id = 'abc123';
await object.fetch({ useMasterKey: true });
// Lets NOT wait for an object to be fetched
const otherObject = new Parse.User();
otherObject.id = 'xyz890';
otherObject.fetch({ useMasterKey: true });
// notice above I am not using await. This will give you the functionality that you're looking for I believe.
});
@Manuel thanks again for the detail around using async in afterSave. My server is a bit quicker now!
And thank you @stevestencil for the example and again for the triggers. They’ve helped me easily compress files, limit each user to one photo and much more. Thank you both for your continued work on Parse and for making my life easier. I cannot stress how much time your work has saved me.
The issue with throwing out of the afterSaveFile isn’t that much of a problem as I can just use return instead of throw.
Glad that you could resolve the issue. If you feel there is something that can be improved in the docs that would have helped you, please feel free to open a PR in the docs repo.