最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - RxJS observable concat not working - Stack Overflow

matteradmin4PV0评论

What is going on with the concat call? I know that if I replace concat by merge the code works correctly and the output is foo bar qux quux. I've read about Hot and Cold observables, and I know that for hot observables that might happen if the values are generated before the subscription, but my observables down there are cold, so I guess that's not the case.

const Rx = require('rxjs');

const observable1 = Rx.Observable.create((observer) => {
  observer.next('foo');
  observer.next('bar');
  return observer;
});
const observable2 = Rx.Observable.create((observer) => {
  observer.next('qux');
  observer.next('quux');
  return observer;
});
const result1 = observable1.concat(observable2);
result1.subscribe((x) => console.log(x));

// outputs
foo
bar

What is going on with the concat call? I know that if I replace concat by merge the code works correctly and the output is foo bar qux quux. I've read about Hot and Cold observables, and I know that for hot observables that might happen if the values are generated before the subscription, but my observables down there are cold, so I guess that's not the case.

const Rx = require('rxjs');

const observable1 = Rx.Observable.create((observer) => {
  observer.next('foo');
  observer.next('bar');
  return observer;
});
const observable2 = Rx.Observable.create((observer) => {
  observer.next('qux');
  observer.next('quux');
  return observer;
});
const result1 = observable1.concat(observable2);
result1.subscribe((x) => console.log(x));

// outputs
foo
bar

https://codepen.io/thiagoh/pen/WZyrRL

Share asked Oct 11, 2017 at 2:31 thiagohthiagoh 7,4188 gold badges54 silver badges78 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

I believe observer1 needs to plete(), then concat can start outputting observer2.

Ammended CodePen

Post a comment

comment list (0)

  1. No comments so far