node.js - Express framework and Promises - what to return from a route handler? -


i have been using following promise-based template express route handlers:

app.get('/accounts/:id', function(req, res) {      accountservice.getaccount(req.params.id)         .then(function(account) {             res.send(account);             //----- must return promise here -----         })         .catch(function(error) {             res.status(500).send({'message': error.tostring()});         }); }); 

while code works fine, uncomfortable onfulfilled function not returning promise. required promise specification: then must return promise. there better way code this?

you've misinterpreted spec.

then must return promise

you confusing return value of then return value of callback then. different.

then must return promise, , is. you're invoking .catch on promise. nothing can can make then not return promise, it's part of implementation of whatever promise library you're using. if library conforms spec, then return promise.

your callback then not have return promise; whatever callback or not return cannot change then's returning of promise, regardless. callback may return promise causes then behave differently, still return promise.

to summarize:

.then( //this must return promise, , it's implementation     function(account) { // (your code) doesn't have return promise        res.send(account);        //----- must return promise here -----        //       ^ no, wrong        //        // we're inside callback being passed then, return value        // of function *not* then's return value   }) 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -