Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Each then() should return a value or throw when using Promises

I have a few async methods that I need to wait for completion before I return from the request. I'm using Promises, but I keep getting the error:

Each then() should return a value or throw // promise/always-return 

Why is this happpening? This is my code:

router.get('/account', function(req, res) {   var id = req.user.uid   var myProfile = {}   var profilePromise = new Promise(function(resolve, reject) {     var userRef = firebase.db.collection('users').doc(id)     userRef.get()       .then(doc => { // Error occurs on this line         if (doc.exists) {           var profile = doc.data()           profile.id = doc.id           myProfile = profile           resolve()         } else {           reject(Error("Profile doesn't exist"))         }       })       .catch(error => {         reject(error)       })   })   // More promises further on, which I wait for }) 
like image 484
Tometoyou Avatar asked Feb 12 '18 16:02

Tometoyou


People also ask

Does then return a Promise?

The then() method returns a Promise . It takes up to two arguments: callback functions for the fulfilled and rejected cases of the Promise .

What does Promise () method do?

The . promise() method returns a dynamically generated Promise that is resolved once all actions of a certain type bound to the collection, queued or not, have ended. By default, type is "fx" , which means the returned Promise is resolved when all animations of the selected elements have completed.

What is Promise resolve () then?

The Promise. resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the value is a thenable, Promise. resolve() will call the then() method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.

Which method returns a Promise?

resolve() method in JS returns a Promise object that is resolved with a given value. Any of the three things can happened: If the value is a promise then promise is returned.


2 Answers

Add at the end of the then()

return null 

That's it.

Each then() should return a value or throw Firebase cloud functions

like image 130
Marlhex Avatar answered Oct 12 '22 06:10

Marlhex


Just avoid the Promise constructor antipattern! If you don't call resolve but return a value, you will have something to return. The then method should be used for chaining, not just subscribing:

outer.get('/account', function(req, res) {   var id = req.user.uid   var userRef = firebase.db.collection('users').doc(id)   var profilePromise = userRef.get().then(doc => {     if (doc.exists) {       var profile = doc.data()       profile.id = doc.id       return profile // I assume you don't want to return undefined //    ^^^^^^     } else {       throw new Error("Profile doesn't exist") //    ^^^^^     }   })   // More promises further on, which I wait for:   // profilePromise.then(myProfile => { … }); }) 
like image 21
Bergi Avatar answered Oct 12 '22 05:10

Bergi