Skip to content

Commit 30602ed

Browse files
committed
add changelog entry for importable waitUntil
1 parent b13ce04 commit 30602ed

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Import `waitUntil` directly without requiring execution context
3+
description: You can now import `waitUntil` from `cloudflare:workers` to extend execution of background tasks without requiring the request context
4+
products:
5+
- workers
6+
date: 2025-08-08
7+
---
8+
9+
You can now import [`waitUntil`](/workers/runtime-apis/context/#waituntil) from `cloudflare:workers` to extend your Worker's execution beyond the request lifecycle from anywhere in your code.
10+
11+
Previously, `waitUntil` could only be accessed through the [execution context](/workers/runtime-apis/context/) (`ctx`) parameter passed to your Worker's handler functions. This meant that if you needed to schedule background tasks from deeply nested functions or utility modules, you had to pass the `ctx` object through multiple function calls to access `waitUntil`.
12+
13+
Now, you can import `waitUntil` directly and use it anywhere in your Worker without needing to pass the execution context as a parameter:
14+
15+
```js
16+
import { waitUntil } from "cloudflare:workers";
17+
18+
// helpers.js - can now use waitUntil without requiring ctx as a parameter
19+
export function trackAnalytics(eventData) {
20+
const analyticsPromise = fetch("https://analytics.example.com/track", {
21+
method: "POST",
22+
body: JSON.stringify(eventData),
23+
});
24+
25+
// Extend execution to ensure analytics tracking completes
26+
waitUntil(analyticsPromise);
27+
}
28+
```
29+
30+
This is particularly useful when you want to:
31+
32+
- Schedule background tasks from utility functions or modules
33+
- Extend execution for analytics, logging, or cleanup operations
34+
- Avoid passing the execution context through multiple layers of function calls
35+
36+
```js
37+
import { waitUntil } from "cloudflare:workers";
38+
39+
export default {
40+
async fetch(request, env, ctx) {
41+
// Background task that should complete even after response is sent
42+
cleanupTempData(env.KV_NAMESPACE);
43+
return new Response("Hello, World!");
44+
}
45+
};
46+
47+
function cleanupTempData(kvNamespace) {
48+
// This function can now use waitUntil without needing ctx
49+
const deletePromise = kvNamespace.delete("temp-key");
50+
waitUntil(deletePromise);
51+
}
52+
```
53+
54+
:::note
55+
The imported `waitUntil` function works the same way as [`ctx.waitUntil()`](/workers/runtime-apis/context/#waituntil). It extends your Worker's execution to wait for the provided promise to settle, but does not block the response from being sent to the client.
56+
:::
57+
58+
For more information, see the [`waitUntil` documentation](/workers/runtime-apis/context/#waituntil).

0 commit comments

Comments
 (0)