NG Observables - suniladhya/Advantage GitHub Wiki
[1] Observable is a wrapper around a data source, i.e. some stream of data. They are used for asynchronous data.
Promise:
Observables | Promises |
Emit multiple values over a period of time. | Emit a single value at a time. |
Are lazy: they’re not executed until we subscribe to them using the subscribe() method. | Are not lazy: execute immediately after creation. |
Have subscriptions that are cancellable using the unsubscribe() method, which stops the listener from receiving further values. | Are not cancellable. |
Provide the map for forEach, filter, reduce, retry, and retryWhen operators. | Don’t provide any operations. |
Deliver errors to the subscribers. | Push errors to the child promises. |
<e-series-collection>
<e-series [dataSource]='chartData' type='Spline' xName='year' yName='sales'> </e-series>
</e-series-collection>`
export class AppComponent {
public chartData: Data[];
constructor(private dataService: DataService) {
}
new Promise((resolve, reject) =>
{
resolve(this.dataService.getData())
}
).then((value : Data[]) =>
this.chartData = value
).catch((err) =>
this.chartData = null
);
}
this.Obs = new Observable((observer) => {
observer.next(this.dataService.getData(true));
setInterval(() => {
observer.next(this.dataService.getData(false));
}, 1000)
});