Observable
Observable provide a way to handle asynchronous communication using a common software pattern by holding a list of subscribers and notify them when changes accord.
To create a observable
const subObserv = new Observable((o) => {
o.next(5);
o.next(10);
o.complete();
})The next method will push a new value to any subscribers, while the complete method tells all the subscribers that the observable is done sending messages.
To receive these messages add a subscriber
subObserv.subscribe({
next(res) {
console.log(`The value is ${res}`);
},
error(err) {
console.error(`Error ${err}`);
},
complete() {
console.log('Completed');
}
})When complete is called the the subscribers will unsubscribed, but if need to do it before than can call unsubscribed()
subObserv.unsubscribed()