2

If you worked with Schedulers you probably know that you can use different already predefined schedulers like queue, async or asap:

of('', queueScheduler)
of('', asyncScheduler)
of('', asapScheduler)

that's all more or less clear.

But what if you want to create your own scheduler, for example: to make a 5s delay?

I could not find any examples/documations about it, except this outdate SO answer - https://stackoverflow.com/a/30921043/274500

Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176

1 Answers1

1

The easiest solution that I found was to extend AsyncScheduler:

class MyScheduler extends AsyncScheduler {
  public schedule<T>(
    work: (this: SchedulerAction<T>, state?: T) => void,
    delay: number = 0,
    state?: T
  ): Subscription {
    return super.schedule(work, delay + 5000, state);
  }
}

const myScheduler = new MyScheduler(AsyncAction);
console.time('myScheduler');
of(1).pipe(observeOn(myScheduler)).subscribe(v => console.timeEnd('myScheduler'));

sandbox - https://stackblitz.com/edit/rxjs-my-scheduler?file=index.ts

Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176
  • May you answer this question? https://stackoverflow.com/questions/67139504/order-of-execution-with-rxjs-asapscheduler – anonymous Apr 17 '21 at 19:01