您如何在rxjs中延迟.forkJoin()
?
这是我所拥有的,但是想要使用delay()运算符吗?
return forkJoin(
this.call1(),
this.call2(),
this.call3()
);
到目前为止,我知道了:
return of(null).pipe(
delay(5000),
switchmap(() => this.call1()),
switchmap(() => this.call2()),
switchmap(() => this.call3()))
);
那是可行的,但是我想使用forkJoin,我尝试了另一种解决方案
return forkJoin(
of(this.call1()).pipe(delay(5000)),
of(this.call2()).pipe(delay(5000)),
of(this.call3()).pipe(delay(5000))
);
但似乎不起作用。
答案 0 :(得分:2)
将delay
运算符与pipe
运算符一起使用
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))
);
答案 1 :(得分:1)
尝试一下
import { delay } from 'rxjs/operators';
return forkjoin(call1(),call2(),call3).pipe(delay(500));
答案 2 :(得分:0)
也许这个更完整的示例将帮助您找到所需的解决方案:
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)
)