Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Forked from jsheely/gist:ff9569a47571e161bd6a
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotlolita/b5fe1d0d3cdfc248f27c to your computer and use it in GitHub Desktop.
Save robotlolita/b5fe1d0d3cdfc248f27c to your computer and use it in GitHub Desktop.
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