Using ES6 Promises, how can I break it in the following scenario?
addClient: function(request, response, next) {
var id = mongo.validateString(request.body.id);
mongo.Test.findOne({
id: id
})
.then(client => {
if (client) {
// want to break here
response.status(400).send({
error: 'client already exists'
});
} else {
return auth.hashPassword(mongo.validateString(request.body.secret));
}
})
.then(hashedSecret => {
// gets executed even if I don't return anything
return new mongo.Test({
name: mongo.validateString(request.body.name),
id: id,
secret: hashedSecret
}).save();
})
.then(doc => {
return response.status(201).send({
client: {
id: doc._id
}
});
})
.catch(err => {
return next(err);
});
}
I haven't found any clear docs stating how to break this.
Instead of having chained then
s I could have it inside the first then
but on more complex requests it would be nice to be able to have them chained.