// routes/starship.js
app.post('/api/starship/unstar', function (req, res, next) {
if (!req.user) return next(errors[21301]);
Starship.forge(
_.pick(req.body, 'id')
).fetch()
.then(function (starship) {
if (!starship) return Promise.reject(errors[20603]);
if (starship.get('userid') != req.user.id) {
return Promise.reject(errors[20102]);
}
return starship.destroy();
}).then(function () {
next({ msg: 'Entity unstarred' });
}).catch(next);
});
// routes/groups.js
function quit(userid, groupid, next) {
return GroupMember.forge({
'userid': userid,
'groupid': groupid
}).fetch()
.then(function (groupMember) {
if (!groupMember) {
return next(errors[40001]);
}
return groupMember.destroy()
.then(function () {
next({
msg: 'quit group success'
});
});
});
}
While using Promise/then, we are encouraged to follow some linear coding (all by my own comprehension)
We all hate "callback hell", don't we? :)