then-all-settled-in
v0.1.0
Published
Constructs a promise of state snapshots for an object whose values are promises.
Downloads
3
Readme
then-all-settled-in
then-all-settled-in
is an object-aware variant of Q's allSettled
method. The allSettledIn
function returns a promise that is fulfilled with a new object containing the state snapshots for each object member. When combined with then-all-in
, this form can be helpful for conditionally queueing asynchronous operations.
$ npm i then-all-settled-in
Example
var allIn = require('then-all-in'),
allSettledIn = require('then-all-settled-in'),
emails = require('./emails');
app.patch('/account/:id', function(request, response, next) {
var body = request.body;
// Assume `emails.validateEmail()` is an asynchronous operation that
// resolves to a Boolean, and `Account.get` constructs a promise for
// an account model.
var operations = allSettledIn({
validation: emails.validateEmail(body.email),
byId: Account.get(request.params.id)
});
operations.then(function(responses) {
var changes = {},
validation = responses.validation,
byId = responses.byId;
if (byId.state == 'rejected')
throw byId.reason;
var account = byId.value;
if (validation.state == 'fulfilled' && validation.value === true && body.email != account.email) {
// Assume `#setEmail()` is asynchronous.
changes.email = account.setEmail(body.email);
}
if (body.phone != account.phone) {
changes.phone = account.setPhone(body.phone);
}
return allIn(changes);
}).done(function() {
response.send(204);
}, next);
});
License
MIT.