Serge van den Oever's weblog

Netlify functions - continue after completion

September 21, 2020 • ☕️ 2 min read

https://docs.netlify.com/functions/overview/ can be used used to provide a API for your web site. You can buid your functions with JavaScript.

There are two common ways to write your Netlify function. The “modern” style and the “legacy” style:

// modern JS style - encouraged
export async function handler(event, context) {
  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Hello world ${Math.floor(Math.random() * 10)}` })
  };
}
// legacy callback style - not encouraged anymore, but you'll still see examples doing this
exports.handler = function(event, context, callback) {
// your server-side functionality
callback(null, {
  statusCode: 200,
  body: JSON.stringify({
    message: `Hello world ${Math.floor(Math.random() * 10)}`
  })
});
};

I would encourage you to use the modern style, and it is required for returning a response to the requester and allowing the function to continue its process. If you use the “legacy” style the response of the callback will not be sent to the requester until the function is done with all invocations or it times out at 10 seconds, even when the setTimeout technique is used as illustrated in the following “modern” style example.

Use the “modern” style like so:

function continueAfterDoneTest.js:

function getTime() {
  return (new Date()).toISOString().substr(11, 8);
}

exports.handler = (event, context) => {
  console.log(`${getTime()}: THE FUNCTION HAS STARTED`);
  setTimeout(function () {
    console.log(`${getTime()}: THE TIMEOUT IS COMPLETED`)
  }, 5000);

 return { statusCode: 200, body: `${getTime()}: FUNCTION COMPLETED` };
};

Function output in the terminal:

◈ Creating Live Tunnel for 8ddb4b97-be43-46aa-855c-0d05999a8ec9

   ┌────────────────────────────────────────────────────────────┐
   │                                                            │
   │   ◈ Server now ready on https://idnv-3ea026.netlify.live   │
   │                                                            │
   └────────────────────────────────────────────────────────────┘

Request from ::1: GET /.netlify/functions/continueAfterDoneTest
12:40:00: THE FUNCTION HAS STARTED
Response with status 200 in 8 ms.
12:40:05: THE TIMEOUT IS COMPLETED

Output in the browser at url https://idnv-3ea026.netlify.live/.netlify/functions/continueAfterDoneTest:

12:40:00: FUNCTION COMPLETED

You will see this output in the browser directly, while the last line in the terminal output comes 5 seconds later.

Discuss on TwitterEdit on GitHub

Serge van den Oever's weblog

Serge van den Oever

Personal blog by Serge van den Oever - als je maar lol hebt...
Twitter: @svdoever
LinkedIn: Serge van den Oever - articles on LinkedIn

Technology Consultant @ Macaw
Competence Team Lead Custom Application Development @ Macaw
2022 Technology Sitecore Most Valuable Professional
2021 Technology Sitecore Most Valuable Professional