export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const client = new Client(env.DATABASE_URL);
await client.connect();
const { rows: [{ now }] } = await client.query('select now();');
ctx.waitUntil(client.end()); // this doesn’t hold up the response
return new Response(now);
}
}
error:
wrangler dev
Delegating to locally-installed [email protected] over global [email protected]...
Run npx wrangler dev to use the local version directly.
http://192.168.1.19:8787
Total Upload: 220.65 KiB / gzip: 54.03 KiB
A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
1:28:37 AM GET / 500
✘ [ERROR] Uncaught (in response) Error: The script will never generate a response.
Creating a new client / calling client.connect() for every request / cron trigger in a Cloudflare worker (or any codebase) is not ideal. I suggest updating the demo to use a connection pool instead. Alternatively, give a second demo with connection pools.