Last active
January 27, 2018 11:22
-
-
Save ichiroku11/903ccef9bb498ea089629d339f0ba6da to your computer and use it in GitHub Desktop.
RxJS scanとreduce
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
| import * as Rx from "@reactivex/rxjs"; | |
| // scan | |
| // 都度nextコールバックが呼ばれる | |
| Rx.Observable.of(1, 2, 3) | |
| .scan((acc, value, index) => acc + value) | |
| .subscribe( | |
| value => console.log(`next: (value: ${value})`), | |
| error => console.log(`error: ${error}`), | |
| () => console.log(`complete:`)); | |
| // 実行結果 | |
| /* | |
| next: (value: 1) | |
| next: (value: 3) | |
| next: (value: 6) | |
| complete: | |
| */ | |
| // reduce | |
| // completeする前に1回だけnextコールバックが呼ばれる | |
| Rx.Observable.of(1, 2, 3) | |
| .reduce((acc, value, index) => acc + value) | |
| .subscribe( | |
| value => console.log(`next: (value: ${value})`), | |
| error => console.log(`error: ${error}`), | |
| () => console.log(`complete:`)); | |
| // 実行結果 | |
| /* | |
| next: (value: 6) | |
| complete: | |
| */ |
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
| import * as Rx from "@reactivex/rxjs"; | |
| // scan | |
| // completeしなくてもnextコールバックが呼ばれる | |
| Rx.Observable.create(observer => { | |
| observer.next(1); | |
| observer.next(2); | |
| observer.next(3); | |
| // completeしない | |
| //observer.complete(); | |
| }) | |
| .scan((acc, value, index) => acc + value) | |
| .subscribe( | |
| value => console.log(`next: (value: ${value})`), | |
| error => console.log(`error: ${error}`), | |
| () => console.log(`complete:`)); | |
| // 実行結果 | |
| /* | |
| next: (value: 1) | |
| next: (value: 3) | |
| next: (value: 6) | |
| */ | |
| // reduce | |
| // completeしない限りnextコールバックが呼ばれない | |
| Rx.Observable.create(observer => { | |
| observer.next(1); | |
| observer.next(2); | |
| observer.next(3); | |
| // completeしない | |
| //observer.complete(); | |
| }) | |
| .reduce((acc, value, index) => acc + value) | |
| .subscribe( | |
| value => console.log(`next: (value: ${value})`), | |
| error => console.log(`error: ${error}`), | |
| () => console.log(`complete:`)); | |
| // 実行結果(何も表示されない) | |
| /* | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment