mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-09 04:22:07 +00:00
25 lines
693 B
JavaScript
25 lines
693 B
JavaScript
/* eslint-disable -- flakey application of `global Response` eslint directive */
|
|
|
|
export const config = { runtime: 'edge' };
|
|
|
|
async function sleep(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
async function doSlowWork(pingUrl) {
|
|
// Wait for 1 second: if this waitUntil promise is not awaited before
|
|
// exiting dev server, the pingUrl won't be fetched.
|
|
await sleep(1000);
|
|
await fetch(pingUrl);
|
|
}
|
|
|
|
export default async (req, ctx) => {
|
|
const pingUrl = req.headers.get('x-ping-url');
|
|
if (!pingUrl) {
|
|
throw new Error('x-ping-url is not set');
|
|
}
|
|
|
|
ctx.waitUntil(doSlowWork(pingUrl));
|
|
return new Response('running waitUntil promises asynchronously...');
|
|
};
|