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

前端 未结 4 1705
粉色の甜心
粉色の甜心 2020-12-05 23:12

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(         


        
4条回答
  •  被撕碎了的回忆
    2020-12-05 23:53

    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 => { … });
    })
    

提交回复
热议问题