deno-express icon indicating copy to clipboard operation
deno-express copied to clipboard

Requests get ended before await

Open BartTactick opened this issue 4 years ago • 2 comments

When i use .then/.catch/.finaly the request gets closed before it could finish the callback.

Example code:

import * as expressive from "https://raw.githubusercontent.com/NMathar/deno-express/master/mod.ts";

const port = 4000; const app = new expressive.App();

app.get("/test", (req, res) => { fetch("http://httpbin.org/get").then(async (e) => { const json = await e.json(); console.log(json);

    res.json(json);
});

});

app.listen(port);

BartTactick avatar Apr 08 '21 12:04 BartTactick

seems like it does work when you put it inside a .use/middleware function

BartTactick avatar Apr 08 '21 13:04 BartTactick

Yes because fetch is also asynchronous so it also needs to be awaited

import * as expressive from "https://raw.githubusercontent.com/NMathar/deno-express/master/mod.ts";

const port = 4000;
const app = new expressive.App();

app.get("/test", async (req, res) => {
  await fetch("http://httpbin.org/get").then(async (e) => {
    const json = await e.json();
    console.log(json);

    res.json(json);
  });
});

app.listen(port);

This does the trick

surajmandalcell avatar Jul 11 '21 12:07 surajmandalcell