Long-Running Asynchronous Processes with Netlify Background Functions
Tags: netlify, development, workflow
Published: 2022-01-03 04:10:07

---
title: Long-Running Asynchronous Processes with Netlify Background Functions
published: true
description: Manage long-running tasks in JavaScript, TypeScript, or Go using asynchronously invoked Netlify Background Functions.
tags: Netlify, Development, Workflow
cover_image: https://cdn.netlify.com/fb9b846dbd25e2c6df49900a4e234fe06966ba70/06594/img/blog/background-functions.png
canonical_url: https://www.netlify.com/blog/2021/12/28/long-running-asynchronous-processes-with-netlify-background-functions/
series: Netlify - a feature a day 2021
---
> Throughout December we'll be [highlighting a different Netlify feature each day](https://www.netlify.com/blog/2021/12/01/highlighting-a-different-netlify-feature-each-day-in-december/?utm_campaign=featdaily21&utm_source=netlify&utm_medium=blog&utm_content=background-functions). It might just be the thing you need to unlock those creative juices, and [dust off that domain](https://www.netlify.com/blog/2021/12/01/dusty-domains-your-forgotten-domains-raise-money-for-charity/?utm_campaign=featdaily21&utm_source=netlify&utm_medium=blog&utm_content=background-functions) you registered but never deployed! Keep an eye [on the blog](https://www.netlify.com/blog/2021/12/01/highlighting-a-different-netlify-feature-each-day-in-december/?utm_campaign=featdaily21&utm_source=netlify&utm_medium=blog&utm_content=background-functions) and on [Twitter](https://twitter.com/netlify) for each feature!

Ain't no serverless functions got time for that! If you noticed, that was a double negative because there is a serverless function that **DOES** have time for your long-running tasks: [Netlify Background Functions](https://docs.netlify.com/functions/background-functions/). You can write these serverless functions in JavaScript, Typescript, or Go and like regular [Netlify Functions](https://docs.netlify.com/functions/overview/), background functions are version-controlled, built, and deployed along with the rest of your Netlify site.

Here's a little sample Background Function in Go that you can try to see a function running longer than 10 seconds:

```go
package main

import (
	"context"
	"log"
	"time"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
	for i := 0; i < 60; i++ {
		log.Println(i)
		time.Sleep(1 * time.Second)
	}
	return nil, nil
}

func main() {
	lambda.Start(handler)
}
```

You can check out some more detailed examples:

* [written in Go](https://github.com/futuregerald/next-function/blob/master/func_source/save.go)
* [written in JavaScript](https://docs.netlify.com/functions/build-with-javascript/#background-function-format)
* or [written in TypeScript](https://docs.netlify.com/functions/build-with-typescript/)

What exactly happens when you use a Background Function? Here are the details about the invocation and responses:

> When a function is invoked asynchronously, there is an initial `202` success response. If function execution returns an error, an execution retry happens after one minute. If it fails again, another retry happens two minutes later. When a background function is successfully executed, you generally pass the result to a destination other than the originating client.

Background Functions are currently in beta but try 'em out, and take your time ;} Happy coding 👩🏻‍💻!

### Resources for the Road

* [Background Function Documentation](https://docs.netlify.com/functions/background-functions/)
* [Create a Background Function](https://docs.netlify.com/functions/build-with-javascript/)