Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an empty promise that just fulfills? [duplicate]

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());     }); 
like image 421
Himmators Avatar asked Jun 28 '16 13:06

Himmators


People also ask

What is an empty promise?

Empty-promise definition (idiomatic) A promise that is either not going to be carried out, worthless or meaningless. noun.

How do I send an empty promise?

An "empty promise" is another way of saying "a lie" in english.. so having an instantly resolved promise is better, just from a language standpoint ;) Promise. resolve() worked for me! new Promise now requires a parameter, use Promise.

Can you return an empty promise?

To return an empty promise with JavaScript, we can use an async function or call Promise. resolve . to create 2 functions that returns empty promises. p1 is an async function, so it's a function that always returns a promise.

Can a promise be resolved twice?

No. It is not safe to resolve/reject promise multiple times. It is basically a bug, that is hard to catch, becasue it can be not always reproducible.


1 Answers

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

like image 66
guest271314 Avatar answered Oct 14 '22 17:10

guest271314