then-all-settled-in
Constructs a promise of state snapshots for an object whose values are promises.
Last updated 7 years ago by kitcambridge .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install then-all-settled-in 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 0.1.0                                ...           latest (7 years ago)

1 Versions

  • 0.1.0                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 4
Dependencies (3)
Dev Dependencies (5)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |