Maps incoming values using given function:
1linkfunction map<I, O>(f: (i: I) => O): (src: Source<I>) => Source<O>
1linkimport { of, map, subscribe, pipe } from 'callbag-common';
2link
3linkpipe(
4link of(1, 2, 3, 4),
5link map(x => x * 2),
6link subscribe(console.log),
7link)
2
4
6
8
👉 The function passed to map()
can also return other callbags (which is called a higher-order callbag). When you do that,
use flatten (or another flattening operator) to turn the higher-order callbag back to normal callbag:
1linkimport {
2link fromEvent, map, interval, flatten, subscribe, pipe
3link} from 'callbag-common';
4link
5linkconst button = document.querySelector('button');
6linkconst span = document.querySelector('span');
7link
8linkpipe(
9link fromEvent(button, 'click'),
10link map(() => interval(100)),
11link flatten,
12link map(i => i / 10),
13link subscribe(i => span.textContent = i)
14link)