Is my assumption correct, that. someSynchronousFunction() might not getting fully executed, because the function has completed by the return statement in the finally-block?
Thing is, someSynchronousFunction() does something that takes time and I don’t want the user to wait for it. In my case its sending Emails with nodeMailer. Sometimes it takes a lot of time, and then I will get this error (locally):
{"errorMessage":"Task timed out after 10.00 seconds","errorType":"TimeoutError","level":"error","stackTrace":["new TimeoutError (/usr/local/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lib/utils.js:112:28)","Context.<anonymous> (/usr/local/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lib/context.js:110:19)","listOnTimeout (node:internal/timers:559:17)","processTimers (node:internal/timers:502:7)"]}
Sometimes it takes so long that this above error appears, sometimes it works within the time.
Locally I will receive the emails, when testing live, the emails doesn’t get sent.
hello @MarcFuSa,
By default, all functions are deployed with:
us-east-1 AWS Lambda region
1024MB of memory
10 second execution limit for synchronous functions
In your case since the EXECUTION LIMIT for Netlify functions is 10 seconds, the execution of the someSynchronousFunction() will NOT complete if it takes more than 10 seconds. Hence the error you are getting.
If you want to run functions for longer periods, Netlify has a new Beta feature called Background Functions.
Netlify’s Background Functions provide an option for serverless functions that run for up to 15 minutes.
You can read more about Background Functions at https://docs.netlify.com/functions/background-functions/
Oh didn’t realize that there are background-functions are different from functions.
From the docs:
Background function syntax is similar to synchronous function syntax, but you will generally pass the function result to a destination other than the originating client.
… “to a destination other than the originating client”
So, how would I send the response to the clientside-function that called this background-function?
The nodemailer-example in the docs doesn’t return anything, just console log the outcome.
@MarcFuSa it is true you will generally pass the function result to a destination other than the originating client.
So for example for background functions you can probably send the result to an email, or forward the raw response to a webhook.
However in your case a background function can still send the emails but you cannot send a response to the function that called the Background function.
Note that the use cases of a regular Netlify function is to perform tasks that do not run for long periods. If your function takes more than 10 seconds then the only option you have is Background function. The downside is that you cannot send a response from the Background function.