0

Why would this piece of code

app.post('/api/v1/subscribe', (req, res) => {
  lsq.services.get('subscribe')
    .then(service => {
      method: 'POST',
      uri: `http://${service}/api/v1/demo/subscribe`,
      json: req.body,
    })
    .then(rp)
});

throw error

 uri: `http://${service}/api/v1/demo/subscribe`,
    ^
SyntaxError: Unexpected token :

My guess is that JS considers { as function opening braces, not object opening braces. So, are we not allowed to directly return an object in a promise?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
Eduard Avetisyan
  • 471
  • 1
  • 4
  • 20

1 Answers1

2

This isn't related to promises at all, only to the ambiguity of the fat arrow function syntax. The problem is the literal object you return is confused with a function body. Just put it between parenthesis:

app.post('/api/v1/subscribe', (req, res) => {
  lsq.services.get('subscribe')
    .then(service => ({
      method: 'POST',
      uri: `http://${service}/api/v1/demo/subscribe`,
      json: req.body,
    }))
    .then(rp)
});
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758