deno oak : howto force reply immediatly, and process the request later ?

Bonjour, My server needs to reply an ACK (200 OK) for each request. If it did not, the client dies. 😱 After the ACK reply, I try my best to process the request. Sometimes it works, sometimes it fails. We don't care.
async function try_doing_something_with(my_req: Request, my_cible: string): void {
// ...
await sleep(555);
}

router.get("/:cible/simple", async (ctx) => {
ctx.response.status = 200;
ctx.response.type = "text/plain";
ctx.response.body = "don't panic, process is started";
// send reply now.
try_doing_something_with(ctx.request, ctx.params.cible);
});
async function try_doing_something_with(my_req: Request, my_cible: string): void {
// ...
await sleep(555);
}

router.get("/:cible/simple", async (ctx) => {
ctx.response.status = 200;
ctx.response.type = "text/plain";
ctx.response.body = "don't panic, process is started";
// send reply now.
try_doing_something_with(ctx.request, ctx.params.cible);
});
In the oak documentation, I saw that I should register a middleware and use next() to call it. But I can't figure how to code this practically. I'm also afraid that the data in ctx.request could be unavailable. Because when my reply is sent, my socket is closed, so obviously my context should be destroyed from memory. Thank you for your help and links. 🙂
AS
Antonio Sampaio45d ago
what about copy the request object and the cible param to the other function? have you tried? how oak treats the async function without await on the middleware?
C
cknight45d ago
The general idea is to kick off an async process without awaiting it and immediately return after the start of that async process. The async process should continue running after the response is returned. You could also use queueMicrotask(() => {}) or setTimeout(() => {}, 0) to put code on the event loop which could, if done correctly, execute after the response is returned. I'm not familiar with Oak however and if it awaits anything during returning of the response (which might then process the event loop ahead of the response being returned to the client).
F
fred41d ago
Thank you. I had tested my skeletton of code : it works. async function(), called without await(), without manual copy of the context parameters, and without using of the oak next() middleware. I think that the memory copy is operated when the function is called. All works well, but I am still a bit confused with the oak middleware documentation... this should be simple detail... Mille mercis!