-
-
Save robotlolita/b5fe1d0d3cdfc248f27c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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