[Rx.js]when does Zip operator emit error?

家住魔仙堡 提交于 2019-12-05 10:39:06

You see the error straight away because the first observable that you pass emits its values synchronously. (The other observables emit their values synchronously, too, but that does not matter in this scenario.)

zip subscribes to the passed observables one by one and in the order in which they are passed. Upon subscribing to the first observable, zip synchronously receives all of the observable's values and the concatenated error. It then emits its own error and is done.

If you specify the optional scheduler argument - so that the observables emit their values asynchronously - you will see the behaviour you were expecting:

var age$ = Rx.Observable.concat(
  Rx.Observable.of(21, 22, 23, Rx.Scheduler.async),
  Rx.Observable.throw("Oop!", Rx.Scheduler.async)
);

var sex$ = Rx.Observable.of(
  "male", "male", "female", "female",
  Rx.Scheduler.async
);

var name$ = Rx.Observable.of(
  "jack", "john", "james", "lucy",
  Rx.Scheduler.async
);

var zipped$ = Rx.Observable.zip(
  age$, sex$, name$,
  (age, sex, name) => ({ age, sex, name })
);
zipped$.subscribe(
  (value) => console.log(value),
  (error) => console.log(error),
  () => console.log("complete")
);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://unpkg.com/rxjs@5/bundles/Rx.min.js"></script>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!