© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Cloudflare DevelopersCD
Cloudflare Developers•2y ago•
2 replies
ink pot monkey ✒🐒

Does ctx.waitUntil work with hyperdrive?

Hello I have the following worker that listens for notifications from a service.
export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext,
  ): Promise<Response> {
    if (request.method !== "POST") {
      return new Response(null, {
        status: 405,
        statusText: "Method Not Allowed",
      });
    }

    const userAgent = request.headers.get("user-agent");
    const data = await request.json();
    const connectionString = env.staging.connectionString;

    console.log(userAgent, data, connectionString);

    if (userAgent?.includes("<example>")) {
      console.log("correct agent")

      const sql = postgres(connectionString, { prepare: false });
      ctx.waitUntil(sql`INSERT INTO payment_logs(user_id) VALUES (999);`);

      return new Response(undefined, { status: 200 });
    }

    return new Response(undefined, { status: 405 });
  },
};
export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext,
  ): Promise<Response> {
    if (request.method !== "POST") {
      return new Response(null, {
        status: 405,
        statusText: "Method Not Allowed",
      });
    }

    const userAgent = request.headers.get("user-agent");
    const data = await request.json();
    const connectionString = env.staging.connectionString;

    console.log(userAgent, data, connectionString);

    if (userAgent?.includes("<example>")) {
      console.log("correct agent")

      const sql = postgres(connectionString, { prepare: false });
      ctx.waitUntil(sql`INSERT INTO payment_logs(user_id) VALUES (999);`);

      return new Response(undefined, { status: 200 });
    }

    return new Response(undefined, { status: 405 });
  },
};


The service disconnects immediately on making the connection and delivering the notification and
ctx.waitUntil
ctx.waitUntil
is not functioning and the database is never updated. Is this a problem with using hyperdrive here? Am I doing something else wrong?

When I test this api endpoint from my machine it works but I do not disconnect immediately like the service
Cloudflare Developers banner
Cloudflare DevelopersJoin
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
85,042Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

calling ctx.waitUntil multiple times
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
4mo ago
Using `ctx.waitUntil()` in SvelteKit environment
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
16mo ago
Suddenly all our ctx.waitUntil workers are failing?
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
10mo ago
Delay ctx.waitUntil() until fetch() has completed or canceled
Cloudflare DevelopersCDCloudflare Developers / workers-and-pages-help
3y ago