marbemac
marbemac
CDCloudflare Developers
Created by marbemac on 4/15/2025 in #workers-help
With wrangler 4, can the using keyword replace the need to manually manage ctx.waitUntil?
For example to manage a db connection, would the below work as expected? And if so, any drawback vs the old waitUntil approach?
const getConnection = async () => {
const connection = await getDb();

return {
connection,
[Symbol.asyncDispose]: async () => {
await connection.close();
},
};
};

// CF worker fetch handler
export default {
async fetch(request, env, ctx) {
await using db = getConnection();

// vs old ctx.waitUntil approach
// ctx.waitUntil(db.connection.close());

return new Response(`Hi`);
},
};
const getConnection = async () => {
const connection = await getDb();

return {
connection,
[Symbol.asyncDispose]: async () => {
await connection.close();
},
};
};

// CF worker fetch handler
export default {
async fetch(request, env, ctx) {
await using db = getConnection();

// vs old ctx.waitUntil approach
// ctx.waitUntil(db.connection.close());

return new Response(`Hi`);
},
};
1 replies