partisani
How to handle a user abort by Deno.serve
try using three backticks to format your code:
const abortController = new AbortController();
Deno.addSignalListener('SIGINT', () => {
abortController.abort();
});
Deno.serve(
{
port:3000,
signal: abortController.signal
},
async (req) => {
if (req.body) {
try {
console.log('Start');
const body = await req.formData();
for (const key of body.keys()) {
console.log(key);
}
console.log('Never reached, when user abort.');
} catch {
// Will not be triggered on abort.
console.log('Error on await req.formData()');
}
}
return new Response("Hello, World!");
}
);
const abortController = new AbortController();
Deno.addSignalListener('SIGINT', () => {
abortController.abort();
});
Deno.serve(
{
port:3000,
signal: abortController.signal
},
async (req) => {
if (req.body) {
try {
console.log('Start');
const body = await req.formData();
for (const key of body.keys()) {
console.log(key);
}
console.log('Never reached, when user abort.');
} catch {
// Will not be triggered on abort.
console.log('Error on await req.formData()');
}
}
return new Response("Hello, World!");
}
);
9 replies