Created
February 28, 2020 06:55
-
-
Save b2977053/5db03ebe9b6de75e7bca1656dae56058 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
// Operators - combineLatest | |
/* | |
同時執行多個 observable 實例, | |
且湊齊所有的 next 才會執行 Callack Function | |
每湊齊一次,執行一次 Callack Function | |
其中一個 observable 只要一有值,就會保留(延續),直到被自己覆蓋。 | |
適合用在多 observable,只會變動其中一個 observable,但又要湊齊變數。 | |
*/ | |
var source = Rx.Observable.interval(500).take(3); | |
var newest = Rx.Observable.interval(300).take(6); | |
var example = source.combineLatest(newest, (x, y) => x + y); | |
//var example = source.combineLatest(newest, x=>x); | |
example.subscribe({ | |
next: (value) => { console.log(value); }, | |
error: (err) => { console.log('Error: ' + err); }, | |
complete: () => { console.log('complete'); } | |
}); | |
// | |
var source = Rx.Observable.interval(500).take(6); | |
var newest = Rx.Observable.interval(500).take(6); | |
var example = source.combineLatest(newest, (x, y) => x + y); | |
//var example = source.combineLatest(newest, x=>x); | |
example.subscribe({ | |
next: (value) => { console.log(value); }, | |
error: (err) => { console.log('Error: ' + err); }, | |
complete: () => { console.log('complete'); } | |
}); | |
//0 | |
//1 | |
//2 | |
//3 | |
//4 | |
//5 | |
//6 | |
//7 | |
//8 | |
//9 | |
//10 | |
/*解釋: | |
6 + 6 = 12, | |
但是要扣掉第一次要等第二次, 所以第一次不會執行 | |
12 - 1 = 11,所以執行11次 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment