Created
November 12, 2011 03:43
-
-
Save mattpodwysocki/1360003 to your computer and use it in GitHub Desktop.
RxJS with combineLatest and zip N parameters
This file contains 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
// Combine Latest | |
var obs1 = Rx.Observable.returnValue(1), | |
obs2 = Rx.Observable.returnValue(2), | |
obs3 = Rx.Observable.returnValue(3); | |
// From Observable with splat | |
Rx.Observable.combineLatest(obs1, obs2, obs3, function(o1, o2, o3) { | |
return o1 + o2 + o3; | |
}).subscribe(function(result) { | |
console.log(result); // 6 | |
}); | |
// From Observable with Array | |
Rx.Observable.combineLatest([obs1, obs2, obs3], function(o1, o2, o3) { | |
return o1 + o2 + o3; | |
}).subscribe(function(result) { | |
console.log(result); // 6 | |
}); | |
// From prototype with splat | |
obs1.combineLatest(obs2, obs3, function(o1, o2, o3) { | |
return o1 + o2 + o3; | |
}).subscribe(function(result) { | |
console.log(result); // 6 | |
}); | |
// From prototype with Array | |
obs1.combineLatest([obs2, obs3], function(o1, o2, o3) { | |
return o1 + o2 + o3; | |
}).subscribe(function(result) { | |
console.log(result); // 6 | |
}); | |
// Zip | |
var obsA = Rx.Observable.fromArray([1,2,3]), | |
obsB = Rx.Observable.fromArray([4,5,6]), | |
obsC = Rx.Observable.fromArray([7,8,9]); | |
// From Observable with splat | |
Rx.Observable.zip(obsA, obsB, obsC, function(a, b, c) { | |
return a + b + c; | |
}).subscribe(function(result) { | |
console.log(result); // 11, 15, 18 | |
}); | |
// From Observable with Array | |
Rx.Observable.zip([obsA, obsB, obsC], function(a, b, c) { | |
return a + b + c; | |
}).subscribe(function(result) { | |
console.log(result); // 11, 15, 18 | |
}); | |
// From Observable with splat and no projection | |
Rx.Observable.zip(obsA, obsB, obsC).subscribe(function(res) { | |
console.log(res.length); // 3, 3, 3 | |
}); | |
// From Observable with Array and no projection | |
Rx.Observable.zip([obsA, obsB, obsC]).subscribe(function(res) { | |
console.log(res.length); // 3, 3, 3 | |
}); | |
// From prototype with splat | |
obsA.zip(obsB, obsC, function(a, b, c) { | |
return a + b + c; | |
}).subscribe(function(result) { | |
console.log(result); // 11, 15, 18 | |
}); | |
// From prototype with splat and no projection | |
obsA.zip(obsB, obsC).subscribe(function(res) { | |
console.log(res.length); // 3, 3, 3 | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rx.Observable.combineLatest doesn't exist... and yet, it should. Why have you forsaken me? :)
Rx.Observable.prototype.combineLatest is no substitute. :)