alepha@docs:~/docs/framework/packages/alepha$
cat background.md | pretty
1 min read
Last commit:

#Alepha - Background

#Installation

Part of the alepha package. Import from alepha/background.

npm install alepha

#Overview

Fire-and-forget background work that should outlive the request that scheduled it, without blocking the response.

Inject BackgroundTaskProvider and call defer(() => …):

ts
1protected readonly background = $inject(BackgroundTaskProvider);2 3createUser = $action({ handler: async ({ body }) => {4  const user = await this.users.create(body);5  this.background.defer(() => this.email.send(welcome(user))); // don't block6  return user;7}});

On Node/Vercel the event loop keeps the task alive. On Cloudflare Workers the workerd build swaps in WorkerdBackgroundTaskProvider, which wraps the task in executionCtx.waitUntil so the isolate isn't frozen at response time - the call site is identical either way.

#API Reference

#Providers