Search code examples
javascriptfirebasereact-nativees6-promise

Don't know how to return sth in a then of a promise


I am using firebase, where I am using some promises (in a react native app).

I am trying to deploy, but ESLINT is complaining that my then clause does not return anything:

admin.auth().getUser(phone)
    .then(userRecord => {
      const code = Math.floor((Math.random() * 8999 + 1000))

      twilio.messages.create({
        body: 'Your code is ' + code,
        to: phone,
        from: '+4915735984308'
      }, (err) => {
        if (err) { return res.status(422).send(err) }

        admin.database().ref('users/' + phone)
          .update({code: code, codeValid: true}, () => {
            res.send({success: true})
          })
      })
    })
    .catch((err) => {
      res.status(422).send({error: err})
    })

The problem is, I don't want to return anything, just write things in the database. I tried the following without success:

.then(userRecord => {
      const code = Math.floor((Math.random() * 8999 + 1000))

      twilio.messages.create({
        body: 'Your code is ' + code,
        to: phone,
        from: '+4915735984308'
      }, (err) => {
        if (err) { return res.status(422).send(err) }

        admin.database().ref('users/' + phone)
          .update({code: code, codeValid: true}, () => {
            res.send({success: true}, () => {return true})
          })
      }, () => {return true})
    })
    .catch((err) => {
      res.status(422).send({error: err}, () => {return true})
    })

Can someone help me with this? Where should I write the return statement?


Solution

  • Just try returning the last line

    admin.auth().getUser(phone)
        .then(userRecord => {
          const code = Math.floor((Math.random() * 8999 + 1000))
    
          return twilio.messages.create({
            body: 'Your code is ' + code,
            to: phone,
            from: '+4915735984308'
          }, (err) => {
            if (err) { return res.status(422).send(err) }
    
            admin.database().ref('users/' + phone)
              .update({code: code, codeValid: true}, () => {
                res.send({success: true})
              })
          })
        })
        .catch((err) => {
          res.status(422).send({error: err})
        })