Leon
Leon8mo ago

How To Add .done method for new Response();

Add a .done method to new Response() to ensure that the response was delivered to the client. function handler(_req) { const res = new Response(null, { headers }); if(res.done) { // ... Execute if the response is guaranteed to be delivered to the client. } return res; } Deno.serve({ port: 80, handler });
5 Replies
Deno AI Helper
Deno AI Helper8mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you've provided, I can offer some observations: 1. The Response object in the Fetch API, which Deno's implementation aligns with, does not have a .done property or method by default. If you want to track whether a response has been fully handled, you would typically need to implement your own logic or use events and streams to determine when the data has been sent. 2. The headers variable in your new Response(null, { headers }) call is not defined within the provided code snippet. Make sure that headers is defined before you use it to avoid a reference error. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of the advice given. If you're looking to ensure that the response was delivered to the client, you might need to look into the underlying transport mechanisms, such as the TCP streams that Deno uses, and see if there are events or callbacks that can inform you when the response has been sent.
Mark G
Mark G8mo ago
Response is based on the web-standard, there is no done method, and therefore highly unlikely that Deno with deviate from the standard. It may help to explain what it is you're trying to do and why you need done.
Leokuma
Leokuma8mo ago
Ugly solution:
Deno.serve(_req => {
let success = true

queueMicrotask(() => {
if (success)
console.log('Success')
else
console.log('Error')
})

try {
return new Response();
} catch (_e) {
success = false;
return new Response();
}
});
Deno.serve(_req => {
let success = true

queueMicrotask(() => {
if (success)
console.log('Success')
else
console.log('Error')
})

try {
return new Response();
} catch (_e) {
success = false;
return new Response();
}
});
Leon
LeonOP7mo ago
In new version 1.43.0 they gave me the answer: "You can use the signal like so:
Deno.serve((req) => {
req.signal.onabort = () => console.log("done");
return new Response("ok");
});
Deno.serve((req) => {
req.signal.onabort = () => console.log("done");
return new Response("ok");
});
It'll be triggered when the response has been fully delivered to the http layer." Is this really how it works? Please look at my comment a little higher - what do you think about it?