Communicating with server using HTTP, Observable & RxJs

Image for post
  1. Callbacks
server.req(reqData, (resData) => {
// asynchronously handles the data
})doExtra() {} // this will execute before the callback.

2. Promises: we can avoid nested callback hell. To handle async operation better.

var promises = http.get(url, data);
promises.then((resData) => {
// Handle data
doExtra() {} // this will execute before the thenreturn promises;

3. Observables:

var obs = http.get(url, data);
// manipulate the observable if desired
obs.subscribe((resData)=> {
//handle data
return obs;

Difference between Promises & Observables:

Promises: Represents the single value that comes back sometimes in the future. Promises are Asynchronous. Cannot deal with Time.

Observables: Represents 0 or more value that comes back immediately or in the future. Observables are Asynchronous as well as synchronous. In observable we can manipulate the data streams.

Improved error handling, can be closed independently of returning a value, Can deal with Time, Supports Advance operators such as filtering, debounce, buffering, distinct, Mathematical Aggregation, Retry, combining observables.