January 2019
Beginner
210 pages
4h 47m
English
Another potential source of items for an observable sequence is a Promise. RxJS also allows us to handle this use case with the from function. We must pass a Promise instance to the from function. In the following example, we use the fetch function to send an HTTP request. The fetch function returns a promise that is passed to the from function:
import { bindCallback } from "rxjs";import fetch from "node-fetch";const uri = "https://jsonplaceholder.typicode.com/todos/1";const observable = from(fetch(uri)).pipe(map(x => x.json()));const subscription = observable.subscribe( (value) => console.log(value.toString()));subscription.unsubscribe();
The generated observable will contain the result of the promise as ...
Read now
Unlock full access