Himmators Himmators - 5 months ago 21
Node.js Question

Create an empty promise that just fulfills?

I have a wrapper that catches the last result of a promise, formats it and outputs the data:

req.resolve = (promise) => {
return promise.then(() => {
res.json(req.user);
}).catch(Sequelize.ValidationError, err => {
// respond with validation errors
return res.status(422).send(err.errors);
}).catch(err => {
// every other error
return res.status(400).send({ message: err.message });
});
};


In one view, I don't have a promise, all that happens is that the auth-function triggers adds req.user and triggers
done()
.

I tried adding a promise like this, but it doesn't get resolved.

app.get('/user/me', auth,
(req, res, next) => {
req.resolve(new Promise());
});

Answer

Promise constructor requires executor function as parameter. Substitute Promise.resolve() for new Promise()

Comments