xifengxx
xifengxx

Reputation: 3

RXJS combineLatest/zip questions

I want to get the console.log result: ["abc", 122, 123], and the operations zip/combineLatest generally fit for my needs.

However, when time or time2 don't have any value, for example: var time = Rx.Observable.of();, then the console.log result isn't ["abc", 122, 123]. So how to solve it?

var time = Rx.Observable.of(['abc']);
var time2= Rx.Observable.of([122,123]);
Rx.Observable.zip(time,time2
                 , (a, b) => [...a, ...b])
  .subscribe(data => console.log("data:",data));
Rx.Observable.merge(time,time2).subscribe(data => {
  console.log("data-M:",data);
})
Rx.Observable.combineLatest(time,time2
                 , (a,b) => {return a.concat(b)})
  .subscribe(data => console.log("data:",data));
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://unpkg.com/@reactivex/[email protected]/dist/global/Rx.js"></script>

</body>
</html>

Upvotes: 0

Views: 181

Answers (1)

Robin Dijkhof
Robin Dijkhof

Reputation: 19288

That is because Rx.Observable.of() creates an empty observable. Both combineLatest and zip will fire when they all have a value. That is why it never fires. Rx.Observable.of('') will work though.

Upvotes: 1

Related Questions