问题
How would you delay .forkJoin() in rxjs?
Here is what I've got but want to use delay() operator with that?
return forkJoin(
this.call1(),
this.call2(),
this.call3()
);
So far I got this:
return of(null).pipe(
delay(5000),
switchmap(() => this.call1()),
switchmap(() => this.call2()),
switchmap(() => this.call3()))
);
That is worked but I would like to use forkJoin, i tried the other soluton
return forkJoin(
of(this.call1()).pipe(delay(5000)),
of(this.call2()).pipe(delay(5000)),
of(this.call3()).pipe(delay(5000))
);
But it seems like not working.
回答1:
use the delay operator withe the pipe operator
import { delay, take } from 'rxjs/operators';
import { forkJoin } from 'rxjs/observable/forkJoin';
import { of } from 'rxjs/observable/of';
return forkJoin(
of(call1()).pipe(delay(1000)),
of(call2()).pipe(delay(2000)),
of(call3()).pipe(delay(1000))
);
回答2:
Try thiz
import { delay } from 'rxjs/operators';
return forkjoin(call1(),call2(),call3).pipe(delay(500));
回答3:
Maybe this more complete example will help you find the solution you need:
import { delay } from 'rxjs/operators';
import { Observable } from "rxjs";
import { forkJoin } from 'rxjs/observable/forkJoin';
function call1(): Observable<any> {
return new Observable<any>(observer => {
setTimeout(_ => {
observer.next('CALL1');
observer.complete();
}, 1000);
})
}
function call2(): Observable<any> {
return new Observable<any>(observer => {
setTimeout(_ => {
observer.next('CALL2');
observer.complete();
}, 2000);
});
}
function call3(): Observable<any> {
return new Observable<any>(observer => {
setTimeout(_ => {
observer.next('CALL3');
observer.complete();
}, 3000);
})
}
forkJoin(call1(), call2(), call3()).pipe(delay(5000)).subscribe(
response => console.log(response)
)
来源:https://stackoverflow.com/questions/50961190/how-to-delay-forkjoin