Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var models = [{
name: 'Jonathan',
location: 'Earth'
}, {
name: 'Joe',
location: 'Mars'
}]
var ps = models.map(function(model) {
// These are two independent operations
var p1 = (model.name)?
nameFormatter.format(model.name)
: Promise.of();
var p2 = (model.location)?
geocorder.geocode(model.location)
: Promise.of();
//Don't fire until async operations have updated the model.
return Promise.all([p1, p2]).then(function([name, location]) {
if (name) model.name = name;
if (location) model.location = location;
return model.save();
})
});
Promises.all(ps).then(...)
var models = [{
name: 'Jonathan',
location: 'Earth'
}, {
name: 'Joe',
location: 'Mars'
}]
var rs = Rx.from(models)
.flatMap(function(model) {
var r1 = (model.name)? Rx.fromNodeCallback(nameFormatter.format)(model.name)
: Rx.Observable.return(null);
var r2 = (model.location)? Rx.fromNodeCallback(geocorder.geocode)(model.location)
: Rx.Observable.return(null);
var r3 = r1.zip(r2, function(name, location) {
if (name) model.name = name;
if (location) model.location = location;
})
r3.flatMap(function(){ return model.save() }) // model.save() needs to return a Rx.Observable
})
rs.subscribeOnCompleted(function() { ... })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment