-
Notifications
You must be signed in to change notification settings - Fork 9.9k
add changelog entry for importable waitUntil #24265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/content/changelog/workers/2025-08-08-add-waituntil-cloudflare-workers.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| --- | ||
| title: Import `waitUntil` directly without requiring execution context | ||
| description: You can now import `waitUntil` from `cloudflare:workers` to extend execution of background tasks without requiring the request context | ||
| products: | ||
| - workers | ||
| date: 2025-08-08 | ||
| --- | ||
|
|
||
| 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. | ||
|
|
||
| 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`. | ||
|
|
||
| Now, you can import `waitUntil` directly and use it anywhere in your Worker without needing to pass the execution context as a parameter: | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```js | ||
| import { waitUntil } from "cloudflare:workers"; | ||
|
|
||
| // helpers.js - can now use waitUntil without requiring ctx as a parameter | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| export function trackAnalytics(eventData) { | ||
| const analyticsPromise = fetch("https://analytics.example.com/track", { | ||
| method: "POST", | ||
| body: JSON.stringify(eventData), | ||
| }); | ||
|
|
||
| // Extend execution to ensure analytics tracking completes | ||
| waitUntil(analyticsPromise); | ||
| } | ||
| ``` | ||
|
|
||
| This is particularly useful when you want to: | ||
|
|
||
| - Schedule background tasks from utility functions or modules | ||
| - Extend execution for analytics, logging, or cleanup operations | ||
| - Avoid passing the execution context through multiple layers of function calls | ||
|
|
||
| ```js | ||
| import { waitUntil } from "cloudflare:workers"; | ||
|
|
||
| export default { | ||
| async fetch(request, env, ctx) { | ||
| // Background task that should complete even after response is sent | ||
| cleanupTempData(env.KV_NAMESPACE); | ||
| return new Response("Hello, World!"); | ||
| } | ||
| }; | ||
|
|
||
| function cleanupTempData(kvNamespace) { | ||
| // This function can now use waitUntil without needing ctx | ||
| const deletePromise = kvNamespace.delete("temp-key"); | ||
| waitUntil(deletePromise); | ||
| } | ||
| ``` | ||
|
|
||
| :::note | ||
| 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. | ||
| ::: | ||
|
|
||
| For more information, see the [`waitUntil` documentation](/workers/runtime-apis/context/#waituntil). | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.