我正在尝试合并两个Obs1和Obs2。 Obs1发送5,6,7。 Obs2发送1,2,3,4。我正在总结-> concat(Obs2,Obs1)
在我的订户中期望1,2,3,4,5,6,7,但只得到1,2,3,4。我究竟做错了什么?

let Obs1 = new rxjs.Subject();
let Obs2 = new rxjs.Subject();

function sendToObs1(x){
    Obs1.next(x)
}

async function sendToObs2(){
  let trns = await getValues();
  for(let i = 0; i < trns.length; i++){
    Obs2.next(trns[i])
  }
  Obs2.complete()
}

function getValues(){
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve([1,2,3,4]), 10)
  })
};

rxjs.concat(Obs2, Obs1).subscribe({
  next: x=> console.log("Received: " + x),
  complete: () => console.log("Done")}
)
sendToObs2()

sendToObs1(5)
sendToObs1(6)
sendToObs1(7)

//Output
// Received: 1
// Received: 2
// Received: 3
// Received: 4


//Expected
// Received: 1
// Received: 2
// Received: 3
// Received: 4
// Received: 5
// Received: 6
// Received: 7
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.1.0/rxjs.umd.js"></script>

最佳答案

Concat仅在Obs2的onComplete之后订阅

let Obs1 = new rxjs.ReplaySubject();
let Obs2 = new rxjs.Subject();

function sendToObs1(x){
    Obs1.next(x)
}

async function sendToObs2(){
  let trns = await getValues();
  for(let i = 0; i < trns.length; i++){
    Obs2.next(trns[i])
  }
  Obs2.complete()
}

function getValues(){
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve([1,2,3,4]), 10)
  })
};

rxjs.concat(Obs2, Obs1).subscribe({
  next: x=> console.log("Received: " + x),
  complete: () => console.log("Done")}
)
sendToObs2()

sendToObs1(5)
sendToObs1(6)
sendToObs1(7)

//Output
// Received: 1
// Received: 2
// Received: 3
// Received: 4


//Expected
// Received: 1
// Received: 2
// Received: 3
// Received: 4
// Received: 5
// Received: 6
// Received: 7
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.1.0/rxjs.umd.js"></script>

09-25 18:46