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
const evenNumber$ = generate({ | |
initialState: 2, | |
condition: x => x <= 10, | |
iterate: x => x + 2 | |
}); | |
evenNumber$.subscribe(number => console.log(number)); | |
// Output: 2, 4, 6, 8, 10 |
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
const endles$ = generate({ initialState: 1, iterate: x => +1 }); |
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 { of } from "rxjs"; | |
// Creating an Observable of numbers | |
const number$ = of(1, 2, 3, 4, 5); | |
// Creating an Observable of strings | |
const pokemon$ = of("Squirtle", "Charmander", "Bulbasur"); | |
// Creating an Observable of arrays | |
const fruit$ = of(["Strawberry", "Cherry"], ["Lemon", "Orange"]); |
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
number$.subscribe(number => console.log(number)); | |
// Output: 1 2 3 4 5 | |
pokemon$.subscribe(pokemon => console.log(pokemon)); | |
// Output: Squirtle Charmander Bulbasur | |
fruit$.subscribe(fruit => console.log(fruit)); | |
// Output: ["Strawberry", "Cherry"] ["Lemon", "Cherry"] | |
iceCream$.subscribe(iceCream => console.log(iceCream)); |
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
// Creating an Observable from an array of strings | |
const fruit$ = from(["Strawberry", "Cherry", "Blackberry"]); | |
// Creating an Observable from a Map object | |
const pokemon$ = from( | |
new Map([ | |
["Squirtle", "Water"], | |
["Charmander", "Fire"], | |
["Bulbasur", "Grass"] | |
]) |
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
letter$.subscribe(letter => console.log(letter)); | |
// Output: "R","x","J","S"," ","i","s"," ","c","o","o","l" | |
fruit$.subscribe(console.log); | |
// Output: "Strawberry", "Cherry", "Blackberry" | |
pokemon$.subscribe(console.log); | |
// Output: ["Squirtle", "Water"], ["Charmander", "Fire"], ["Bulbasur", "Grass"] | |
promise$.subscribe(promise => console.log(promise)); |
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
const fruitsOf$ = of(["Strawberry", "Cherry"], ["Lemon", "Orange"]); | |
const fruitsFrom$ = from(["Strawberry", "Cherry", "Blackberry"]); | |
fruitOf$.subscribe(fruit => console.log(fruit)); | |
// Output: ["Strawberry", "Cherry"] ["Lemon", "Cherry"] | |
fruitFrom$.subscribe(console.log); | |
// Output: "Strawberry", "Cherry", "Blackberry" |
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
click$.subscribe(click => console.log(click)); | |
// Output: MouseEvent {isTrusted: true} | |
keyPressed$.subscribe(console.log); | |
// Output: KeyboardEvent {isTrusted: true} | |
scroll$.subscribe(scroll => console.log(scroll)); | |
// Output: UIEvent {isTrusted: true} | |
copie$.subscribe(console.log); |
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 { interval } from "rxjs"; | |
// Observable will emit incremental numbers, one every second (1000ms) | |
const number$ = interval(1000); | |
// We didn't pass the interval size parameter, so default value is 0ms | |
const superFastNumber$ = interval(); | |
number$.subscribe(console.log); |
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 { timer } from "rxjs"; | |
// Observable will emit one value (0) after 2s and complete | |
const onlyOneValue$ = timer(2000); | |
// Observable will wait 5s, then start emitting values every second | |
const number$ = timer(5000, 1000); | |
onlyOneValue$.subscribe(console.log); |