Local development with Defer

An unified experience, from your local machine to production.


Prerequisites


 

 

 

Overview

Locally, background functions run in the background leveraging the Promise API, providing a behavior similar to production environments (with some Limitations).

No additional package or local server is required, simply call your background functions and they will run locally, as expected:

defer/helloWorld.ts
// the `defer()` helper will be used to define a background function
import { defer } from "@defer/client"
 
// a background function must be `async`
async function helloWorld(name: string) {
    return new Promise(
    (resolve) => {
        setTimeout(() => {
        console.log(`Hello ${name}!`)
        resolve('done')
        }, 5000)
    }
    )
}
 
// the function must be wrapped with `defer()` and exported as default
export default defer(helloWorld)

 

What happen if I set a DEFER_TOKEN locally?

We don't recommend doing it.

By setting a DEFER_TOKEN locally, all calls to your background functions will run them on the Defer Platform.

It will result in executing the version of your background function present in Git, not locally, which can be confusing.

 

 

 

Advanced example: get an execution's result locally

The awaitResult() and getExecution() APIs work as expected locally, returning the status or result of an ongoing or finished local execution.

A complete working example is available on our GitHub:


 

 

 

Limitations

 

API limitations

The following @defer/client APIs are not yet supported locally:

  • delay(): will result in executing the function immediately
  • defer.cron(): CRONs are not scheduled locally

 

Behavior limitations

Defer relies on the Promise API to execute background functions locally.

It means that all executions are bound to the parent process and will be cancelled if the parent process (ex: server) is force stopped.