RxJS dependency issue with Scheduler.async
yes, this is correct, because:
import { Scheduler } from 'rxjs/Scheduler';
this mean, you imported this class: https://github.com/ReactiveX/rxjs/blob/5.4.0/src/Scheduler.ts#L8-L63
and
import { async } from 'rxjs/scheduler/async';
is https://github.com/ReactiveX/rxjs/blob/5.4.0/src/scheduler/async.ts#L47
So you can see, Scheduler
does not have async
property, I guess you wanna convert this thing Rx.Scheduler.async
source code here, you could try this solution:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/observeOn';
import { async } from 'rxjs/scheduler/async';
var observable = Observable.create(function (observer) {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
}).observeOn(async);
I think this deserves a little deeper explanation.
If you write just:
import { Scheduler } from 'rxjs/Scheduler';
you're importing just the Scheduler
class that you probably never want to use by itself. It's useful to import this only for proper type checking so you can have for example:
import { Scheduler } from 'rxjs/Scheduler';
class MyClass {
sched: Scheduler;
}
But importing only async
you're importing an already existing instance of an AsyncScheduler
class. This is serves as a singleton pattern because there's usually no need to have multiple AsyncScheduler
instances.
import { async } from 'rxjs/scheduler/async';
Now you can for example check what is the current time for this particular Scheduler:
async.now();
In fact importing from rxjs/scheduler/async
is the same as using Scheduler
imported from just rxjs
which is in fact Rx.ts
:
import { Scheduler } from 'rxjs';
...
Scheduler.async.now();
You can see there are more different Schedulers already prepared: https://github.com/ReactiveX/rxjs/blob/master/src/Rx.ts#L193-L198
What's confusing here is what is the difference between using async
from 'rxjs/scheduler/async'
and Scheduler.async
from 'rxjs'
. The async
from Rx.ts
just imports the same 'rxjs/scheduler/async'
but since you're importing rxjs
you're also including all the dependencies defined in Rx.ts
. This means you're importing all the operators which is a lot of files that you probably don't need.
That's why it's better to import it only from 'rxjs/scheduler/async'
and not 'rxjs'
:
import { async } from 'rxjs/scheduler/async'
I know that this is tagged for typescript, angular and rxjs5, but if you landed here and are confused as to why the other answers aren't working for you, I think the location of the schedulers moved again in v6. I have a webpack development environment and I had to get the schedulers off of the bare rxjs
module. You can see all of the schedulers if you inspect the module that gets imported:
import * as rxjs from "rxjs";
console.log(Object.keys(rxjs).filter(k => k.includes("Scheduler")));
Will print:
"asapScheduler"
"asyncScheduler"
"queueScheduler"
"animationFrameScheduler"
"VirtualTimeScheduler"
"Scheduler"
So if you want async
Scheduler you can do:
import {asyncScheduler} from "rxjs";