-1

Promise is an api given by browser to do time consuming time in asyn manner.Afetr completion of event loop then function is executes in promise.Does observables also handled by browser in same way that is it is also handled by browsers?How observables is internally implemented to behave async?

1 Answers1

0

Observables are not async by nature - they are just push based. For example:

console.log(1);
Observable.of(1).subscribe(() => console.log(2));
console.log(3);

Will log 1,2,3 and not 1,3,2 like:

console.log(1);
Promise.resolve(1).then(() => console.log(2)); // then is `async`
console.log(3);

Observables can use a scheduler on the other hand which uses a source of asynchronisity from the browser itself - like setTimeout. You can configure Rx's scheduler by passing another parameter to Rx functions (like from(10, Rx.scheduler.asap)).

Promise libraries existed before native promises though. At that point they used setTimeout, message channels, mutation observers or other hacks to get a source of asynchronisity.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504