Hi,
I noticed that some of my users who use products depending on serverless functions are getting an error response after about 40 seconds, even though I set the timeout to the maximum value of 900 seconds.
However, I decided to create a dummy function that waits for 50 seconds and then returns a Hello World
response. The result is that after about 40 seconds, it returns a 202
status code with the response body Response not yet ready.
How can I make the function wait until the real promise finishes and then return the result, or at least wait until the timeout that I set in the function settings?
The dummy function code:
async function main(args) {
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
await wait(50000);
return {"body": "Hello World!"};
}
The timeout settings
The result of the execution of the function after about 40 seconds
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hey!
I’ve just tested this out, and it looks like the DigitalOcean’s serverless platform automatically demotes functions that run longer than 30 seconds to asynchronous invocations. This means your function will start running synchronously but will switch to an asynchronous mode if it crosses the 30-second threshold:
When your function gets demoted to asynchronous, it immediately returns an activation ID (the
code
field in the JSON output that you’ve shared). This ID is crucial as it allows you to fetch the function’s result once it has completed its execution.To get the output of your long-running function, you can use the
doctl
command or make another REST API call. Withdoctl
, you can retrieve the activation record using the returned activation ID:Or for just the function result:
Here is an example of using
curl
, where the approach is similar. You’ll need to make a GET request to the activations endpoint of the DigitalOcean Functions REST API:If your function is still executing when you query its result, you’ll get an error message indicating that the activation record does not exist yet. In this case, just wait a bit longer before trying again.
For more information, I would recommend checking out the documentation here:
Hope that this helps and let me know if you have any further questions!
Best,
Bobby
Just wanted to give an update that I tried to execute the function from the control panel. It waited until the function was done and returned the target response
Hello World
successfully. I’m wondering why it works from the control panel and not from other methods!?