0

See, a promise have two parameters resolved and rejected. So by calling then, you are passing it 2 parameters.

router.route('/issue/add').post((req,res) => 
{
    let issue= new Issue(req.body);

    issue.save().then((issue) => 
        {
            res.status(200).json({'issue': 'Added Successfully'});
        }).catch(err => 
        {
            res.status(400).send('Failed to create a requested record');
        });
});

but in the code above issue is passed and mapped to resolved but for rejected an err param need to be passed in then but instead it is passed in catch and works. Why?

Jameela Khan
  • 69
  • 1
  • 5
  • what do you mean it works? it can't work for both. Either status 200 will be responded or 400, won't be both – Adelin Sep 28 '18 at 10:20
  • `issue` is the resolved value of a fulfilled promise ... `err` is the rejection reason for a rejected promise ... the names of the function parameters are not at all relevant ... the `issue` of `let issue` is **NOT** the same thing as the `issue` in `.then((issue)` ... in other words `.then((result) =>` would mean the argument named `result` has the result of a successful `.save` – Jaromanda X Sep 28 '18 at 10:31
  • `So by calling then, you are passing it 2 parameters` - yes, `.then` takes two arguments, an onfulfilled function and an onrejected function ... however your code doesn't even do that – Jaromanda X Sep 28 '18 at 10:34
  • Both parameters of `then` are optional. You don't have to pass a `onreject` callback. – Bergi Sep 28 '18 at 13:09
  • "*a promise have two parameters resolved and rejected.*" - what do you mean? A promise is an object, not a function, it doesn't have any parameters at all. – Bergi Sep 28 '18 at 13:10

0 Answers0