简体   繁体   English

RXJS concat:确保 pipe 中的逻辑在 observable 完成之前执行

[英]RXJS concat: Ensuring logic in pipe executes before observable completes

From: https://www.learnrxjs.io/learn-rxjs/operators/combination/concat来自: https://www.learnrxjs.io/learn-rxjs/operators/combination/concat

Subscribe to observables in order as previous completes按照之前完成的顺序订阅 observables

You can think of concat like a line at a ATM, the next transaction (subscription) cannot start until the previous completes!您可以将 concat 想象成 ATM 上的一条线,下一个事务(订阅)在前一个事务完成之前无法开始!

Given the above I'd expect the following example to log as 1, '2', '3', 4. When I run this I'm getting 1, 4, '3', '2'.鉴于上述情况,我希望以下示例记录为 1、'2'、'3'、4。当我运行它时,我得到 1、4、'3'、'2'。 Is there a way to ensure the piped logic has run before completing?有没有办法确保管道逻辑在完成之前已经运行?

import { concat, of } from 'rxjs';
import { map } from 'rxjs/operators';

const o1o2 = of(console.log('1')).pipe(map(x => console.log(2)));
const o3 = () => console.log(3);
const o4 = of(console.log('4'));

concat(o1o2, of(o3()), o4).subscribe();

https://stackblitz.com/edit/rxjs-bqr9pr?file=index.ts https://stackblitz.com/edit/rxjs-bqr9pr?file=index.ts


Edited:编辑:

In the above 1 and 4 were fired immediately when the lines were executed (before even hitting concat).在上面的 1 和 4 中,当行被执行时立即被触发(甚至在点击 concat 之前)。

3 was fired because (of) creates a hot observable where logic happens before the observable returns. 3 被触发是因为 (of) 创建了一个热可观察对象,其中逻辑发生在可观察对象返回之前。 Similar scenario as above actually.实际上与上面类似的场景。 As concat chains the observables before executing sequentially, so we see 3 logged.由于 concat 在顺序执行之前链接了 observables,所以我们看到记录了 3 个。 Defer is a way to make the observable cold. Defer 是一种使 observable 变冷的方法。

2 was the only thing that was dependent on a subscription to log. 2 是唯一依赖于订阅日志的东西。

o4 = of(console.log('wut')).pipe(tap() => console.log(4))
also3 = defer() => {
    console.log(3)
    return of()
}
o3 = defer(() => {
    return of(console.log(3))
})
o2 = of(null).pipe(tap () => console.log(2))
o1 = of(null).pipe(tap () => console.log(1))

concat(o1, o2, o3, o4);

The below example would log wut, 1, 2, 3, 4下面的示例将记录 wut, 1, 2, 3, 4

Let's take this line-by-line:让我们逐行看:


const o1o2 = of(console.log('1')).pipe(map(x => console.log(2)));

When you call of on this line, you immediately evaluate console.log('1') and pass the result of the log function to of (the result of log is undefined ).当您在此行调用of时,您会立即评估console.log('1')并将log function 的结果传递给oflog的结果是undefined )。

This logs '1' to the console.这会将'1'记录到控制台。


const o4 = of(console.log('4'));

Again, you are immediately evaluating console.log('4') and passing the result of that function, undefined , as the first argument to of同样,您正在立即评估console.log('4')并将 function, undefined的结果作为第一个参数传递给of

This logs '4' to the console.这会将'4'记录到控制台。


concat(o1o2, of(o3()), o4).subscribe();

On this line, you are invoking o3 immediately and using the result of that function as the first argument for of .在这一行中,您将立即调用o3并将 function 的结果用作of的第一个参数。 As o3 calls console.log(3) and returns its result, you are simply invoking of() with undefined .o3调用console.log(3)并返回其结果时,您只需使用undefined调用of()

This logs 3 to the console.这会将3记录到控制台。


const o1o2 = of(console.log('1')).pipe(map(x => console.log(2)));

And we're now back to this line, which is, I think, the only one that is behaving as you were expecting because x => console.log(2) is a function, so it doesn't evaluate console.log until it's called.现在我们回到这一行,我认为,这是唯一一个表现如您所料的行,因为x => console.log(2)是 function,所以它不会评估console.log直到它被称为。 I don't know the RXJS library well, but since your second log call is wrapped in a function, it appears that concat is evaluating it while doing whatever it is that concat does.我不太了解 RXJS 库,但是由于您的第二个log调用包含在 function 中,因此concat似乎正在评估它,同时执行concat所做的任何事情。

This logs 2 to the console.这会将2记录到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM