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`);
},
};
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?