Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rxjs Scheduler: What's the difference between asap and async?

Tags:

rxjs

Could somebody tell me the difference between Scheduler.asap and Scheduler.async?

Looks the same for me:

const observable = Observable.create(function (observer) {
  observer.next(1);
  observer.next(2);
  observer.next(3);
  observer.complete();
})
.observeOn(asap);
//.observeOn(async);

console.log('just before subscribe');
observable.subscribe({
  next: x => console.log('got value ' + x),
  complete: () => console.log('done'),
});
console.log('just after subscribe');

Returns:

just before subscribe
just after subscribe
got value 1
got value 2
got value 3
done

Code - https://stackblitz.com/edit/rxjs-85vczc?file=app/hello.component.ts

like image 228
Stepan Suvorov Avatar asked Oct 27 '25 10:10

Stepan Suvorov


1 Answers

From: Randall Koutnik Book “Build Reactive Websites with RxJS.” :

RxJS has two different types of asynchronous schedulers: asap and async. The key difference is that asap schedules the observable event to be run using the micro task queue (process.nextTick() in Node.js, or setTimeout in the browser). This means that the event runs after any synchronous code but before any other scheduled asynchronous tasks. The other major scheduler, async, schedules using setTimeout, and is appropriate for some time-based operations.

like image 125
H S Progr Avatar answered Oct 29 '25 08:10

H S Progr