Rxjs共享运算符未给出预期结果

时间:2018-10-01 00:21:43

标签: rxjs rxjs6

我正在尝试使rxjs运算符共享像https://www.learnrxjs.io/operators/multicasting/share.html中那样工作,但是没有得到预期的结果。我期望“运行地图功能”仅记录一次。我想念什么吗?

const { of } = rxjs;
const { map, share } = rxjs.operators;

const obs$ = of('data');

const mapped$ = obs$.pipe(map(d => {
  console.log('Running map function');
  return `mapped ${d}`;
}));

const shared$ = mapped$.pipe(share());

shared$.subscribe(d => { console.log(d); });

shared$.subscribe(d => { console.log(d); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.3.3/rxjs.umd.min.js"></script>

1 个答案:

答案 0 :(得分:1)

如上所述,您的订阅已同步。要获得预期的行为,可以使用调度程序。首先,您可以阅读this article,例如

bluebird
node_modules/u/dist/src/lib/dynamo/Dynamo.d.ts