Question

Function Returns Response After 38 Seconds and Ignores the Timeout Settings

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

image alt text
Function’s Timeout Settings

The result of the execution of the function after about 40 seconds

image alt text
The result of the execution

Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
December 23, 2023
Accepted Answer

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:

https://docs.digitalocean.com/products/functions/how-to/async-functions/

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. With doctl, you can retrieve the activation record using the returned activation ID:

doctl serverless activation get <your-activation-id>

Or for just the function result:

doctl serverless activation result <your-activation-id>

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:

curl -X GET "https://<your-server>.doserverless.co/api/v1/namespaces/<your-namespace>/activations/<your-activation-id>" \
    -H "Authorization: Basic <your-token>"

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:

https://docs.digitalocean.com/products/functions/how-to/async-functions/

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!?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel