The original post for reference is here: Express.js Response Timeout
I'm trying to incorporate a timeout here, the response takes 10 sec and the timeout will be thrown in 5 sec, just for testing
const express = require('express');
const app = express();
const port = 3000;
app.use(function(req, res, next){
res.setTimeout(5000, function(){
console.log('Request has timed out.');
return res.json({succes: false, message: "request timeout"});
});
console.log("calling next");
next();
});
const asyncWork = () => {
return new Promise((resolve,reject) => {
setTimeout(() => {
console.log("after 15 sec");
resolve({"id": 1});
},10000)
});
};
app.get('/', async(req, res) => {
const data = await asyncWork();
res.json(data);
});
const server = app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
})
I'm able to achieve the request timeout message but it's also trying to execute and the response from asyncWork which throws an uncaughtPromiseRejectionWarning which I think is because it's trying to send 2 response.
I'm new to express so any suggestion or advice would help