RxJS (Reactive
Extensions for JavaScript) is a library for reactive programming using
observables that makes it easier to compose asynchronous or callback-based
code. RxJS provides an implementation of the Observable type.
RxJS offers a number of functions that can be used to create new observables. These functions can simplify the process of creating observables from things such as events, timers, promises, and so on.
Create an Observable that will create an AJAX request
RxJS offers a number of functions that can be used to create new observables. These functions can simplify the process of creating observables from things such as events, timers, promises, and so on.
Create an Observable out of a promise
import { from } from 'rxjs';
const data = from(fetch('/api/endpoint'));
// Subscribe to begin listening for async result
data.subscribe({
next(response) { console.log(response); },
error(err) { console.error('Error: ' + err); },
complete() { console.log('Completed'); }
});
// Subscribe to begin listening for async result
data.subscribe({
next(response) { console.log(response); },
error(err) { console.error('Error: ' + err); },
complete() { console.log('Completed'); }
});
Create an Observable that will create an AJAX request
import { ajax } from 'rxjs/ajax';
const apiData = ajax('/api/data');
// Subscribe to create the request
apiData.subscribe(res => console.log(res.status, res.response));
// Subscribe to create the request
apiData.subscribe(res => console.log(res.status, res.response));
No comments:
Post a Comment