Professional Documents
Culture Documents
What Is RXJS?: Next Error Complete
What Is RXJS?: Next Error Complete
// Execute with the observer object and Prints out each A handler that implements the Observer interface for
item
myObservable.subscribe(myObserver); receiving observable notifications will be passed as a
// => Observer got a next value: 1 parameter for observable as below,
// => Observer got a next value: 2
// => Observer got a next value: 3
myObservable.subscribe(myObserver);
// => Observer got a next value: 4
// => Observer got a next value: 5
// => Observer got a complete notification
Note: If you don't supply a handler for a notification
type, the observer ignores notifications of that type.
3. What is an observable?
5. What is the difference between promise
An Observable is a unique Object similar to a Promise
and observable?
that can help manage async code. Observables are
not part of the JavaScript language so we need to rely
Below are the list of differences between promise and
on a popular Observable library called RxJS. The
observable,
observables are created using new keyword. Let see
the simple example of observable,
45. What is multicasting?
import { Observable } from 'rxjs';
const observable = new Observable(observer => { Multi-casting is the practice of broadcasting to a list
setTimeout(() => {
observer.next('Hello from a Observable!'); of multiple subscribers in a single execution. Let's
}, 2000); demonstrate the multi-casting feature,
});
46. How do you perform error handling in ii. Create an observable that creates an AJAX
observables? request
You can handle errors by specifying an error import { ajax } from 'rxjs/ajax'; // ajax function
const apiData = ajax('/api/data'); // Created from AJAX
callback on the observer instead of relying on request
try/catch which are ineffective in asynchronous // Subscribe to create the request
environment. For example, you can define error apiData.subscribe(res => console.log(res.status,
res.response));
callback as below,
The subscribe() method can accept callback function import { fromEvent } from 'rxjs';
const el = document.getElementById('custom-element');
definitions in line, for next, error, and complete const mouseMoves = fromEvent(el, 'mousemove');
handlers is known as short hand notation or Subscribe const subscription = mouseMoves.subscribe((e:
MouseEvent) => {
method with positional arguments. For example, you console.log(`Coordnitaes of mouse pointer:
can define subscribe method as below, ${e.clientX} * ${e.clientY}`);
});
myObservable.subscribe(
x => console.log('Observer got a next value: ' + x),
err => console.error('Observer got an error: ' + err), 50. What will happen if you do not supply
() => console.log('Observer got a complete
notification') handler for observer?
);