subscribeOn() { // Changes source execution // only used once of(1).pipe( subscribeOn(async) ) .subscribe({ next: x => console.log(x), complete: () => console.log('3') }) console.log('2') //2 1 3 } observeOn() { // Changes notifications execution (next, error, complete) // Can be used before each operators } queueSchedular() { // execute synchronously // tasks execute in order // waits until current task ends before exeucting next one // performant (precedes event loop) queue.schedule(() => console.log(1)) // sync console.log(2) // sync queue.schedule(() => console.log(3)) // sync // 1 2 3 queue.schedule(() => { // when nested queue, change behavior queue.schedule(() => console.log(1)) // async console.log(2) // sync queue.schedule(() => console.log(3)) // async }) // 2 1 3 } asapSchedular() { // executes asynchronously (micro) // Tasks execute before next tick // Relays on Promises // Performant (precedes event loop) setTimeout(() => console.log(1)) // macro asap.schedule(() => console.log(2)) // micro queue.schedule(() => console.log(3)) // snyc // 3 2 1 } asyncSchedular() { // Executes asynchronously (macro) // Relays on setInterval // less performant (use event loop) async.schedule(() => console.log(1)) // macro asap.schedule(() => console.log(2)) // micro queue.schedule(() => console.log(3)) // snyc // 3 2 1 } cancelling() { const s = new AsyncScheduler(AsyncAction) const DELAY = 0; let subscription = s.schedule(v => console.log(v), DELAY, '1') s.schedule(v => console.log(v), DELAY, '2') console.log('3') subscription.unsubscribe(); // 3 2 // 1 got cancelled } internalTime() { const s = new AsyncScheduler(AsyncAction) const DELAY = 2000; const start = Date.now() s.schedule(v => console.log(v), DELAY, 1) s.schedule(v => console.log(v), DELAY, 2) s.schedule(v => console.log(`${s.now() - start}ms`), DELAY) console.log(3) // 3 1 2 2008ms }