我有两个电话。第一次呼叫和第二次呼叫。
第二次调用需要更长的99%的时间,一旦它完成,就有了我需要的所有信息。但是为了安抚用户,我打了第一个电话,它是快速和有限的信息。
如果第二次调用在第一次调用之前完成-取消第一次调用。
我该如何实现这一点?
let myResult$ = someOperator([
myApiService.call1(),
myApiService.call2()]).pipe(
//do something after each call completes (twice in Total)
);发布于 2020-08-09 20:04:20
正如Oles Savluk正确指出的那样:
let myResult$ = merge([
myApiService.call1(),
myApiService.call2()
]);请注意,它会在可观察对象中产生两个单独的值。如果您需要将两个请求的结果放在一个值数组中,请使用forkJoin。不要忘记订阅/取消订阅myResult$。
编辑:未测试,但如果您想要赋予其中一个观察值更高的优势:
let superior$ = myApiService.call1();
let inferior$ = myApiService.call2();
let myResult$ = merge([superior$, inferior$]).pipe(
takeUntil(superior$)
);merge确保这两个观察点都得到了他们的拍摄,而他takeUntil确保如果上级先来,结果将立即结束。
发布于 2020-08-13 03:52:13
选项1.
我喜欢@Wouter van Koppen的asnwer,但很少有什么改变会让它变得更好:
const long$ = myApiService.longOperation().pipe(share())
const short$ = myApiService.shortOperation$.pipe(takeUntil(long$))
merge(long$, short$).subscribe(console.log);使用share是为了让takeUntil(long$)和merge(long$共享相同的订阅,从而防止longOperation被订阅两次。
选项2.
shortOperation$
.pipe(
takeUntil(
longOperation$.pipe(
tap(x => {
// run some code
})
)
)
)
.subscribe(console.log);https://stackoverflow.com/questions/63321116
复制相似问题