Converts a promise to a callbag source.
1linkfunction fromPromise<T>(promise: Promise<T>): Source<T>
1linkimport { fromPromise, subscribe, pipe } from 'callbag-common';
2link
3linkpipe(
4link fromPromise(
5link fetch('https://api.github.com/repos/loreanvictor/callbag-common')
6link .then(res => res.json())
7link ),
8link subscribe(repo => {
9link console.log(repo.stargazers_count);
10link })
11link);
5
👉 fromPromise()
is typically used alongside flatten for turning
user input into requests:
1linkimport {
2link fromPromise, fromEvent, flatten, subscribe, pipe, map,
3link} from 'callbag-common';
4link
5linkconst input = document.querySelector('input');
6linkconst button = document.querySelector('button');
7linkconst span = document.querySelector('span');
8link
9linkconst getRepo = async (repo) => {
10link try {
11link const res = await fetch('https://api.github.com/repos/' + repo);
12link return await res.json();
13link } catch {}
14link}
15link
16linkpipe(
17link fromEvent(button, 'click'),
18link map(() => fromPromise(getRepo(input.value))),
19link flatten,
20link map(repo => repo?.stargazers_count || '??'),
21link subscribe(count => span.textContent = count)
22link);