简体   繁体   English

在维护订单的订阅内订阅

[英]Subscribe inside a subscribe maintaining the order

Below coding has subscribe inside a subscribe.下面的代码在订阅中有订阅。 It gets a schedule_id and according to the schedule_id retrieve questions.它获取一个schedule_id 并根据schedule_id 检索问题。 This works perfectly but the order of getQuestion() is not guaranteed.这非常有效,但不能保证getQuestion()的顺序。

schedule_id#: 111, 222, 333, 444 schedule_id#: 111, 222, 333, 444

getQuestion(schedule_id) output#: q1(222), q23(111), q12(444), q15(333) getQuestion(schedule_id) 输出#: q1(222), q23(111), q12(444), q15(333)

But it should be#: q23(111), q1(222), q15(333), q12(444)但它应该是#: q23(111), q1(222), q15(333), q12(444)

I need to execute getQuestion() according to the order of schedule_ids, wait until one process finishes before go to the next.我需要按照 schedule_ids 的顺序执行getQuestion() ,等到一个进程在 go 之前完成到下一个。 Is it possible to use forkJoin() here?可以在这里使用forkJoin()吗?

const source = from(res.data);
source.pipe(map(({ schedule_id }) => schedule_id))
  .pipe(mergeMap(schedule_id => {
    let param = {
      "schedule_id": schedule_id
    }
    console.log("schedule_id#: ", schedule_id);
    return this.fetchApiService.getQuestion(param);
  }))
  .subscribe((result) => {
    console.log('result#:', result);
  })

ConcatMap works just fine for me. ConcatMap 对我来说工作得很好。 Of course, with concatMap, you will never start the next observable until the previous one completes (that's how you maintain order).当然,使用 concatMap 时,您永远不会启动下一个 observable,直到前一个 observable 完成(这就是您维持顺序的方式)。 If you hand it an observable that never completes, you end up with a minor memory leak and nothing else.如果你给它一个永远不会完成的可观察对象,你最终会得到一个小的 memory 泄漏,没有别的。

If I take you code and make a lawful observable, it works for me.如果我给你编写代码并进行合法的观察,它对我有用。

  myConcatMapFunc(){
    let schedule_ids = from(['111','222','333','444']);
    schedule_ids.pipe(
      concatMap(data => {

        let param = {
          "schedule_id": data,
        }

        console.log('schedule_id#', data);

        return new Observable(subscriber => {
          subscriber.next('Q1');
          subscriber.next('Q2');
          subscriber.next('Q3');
          subscriber.complete();
          return { unsubscribe: () => {} };
        });

      })
    ).subscribe(result => {
      console.log('result#', result);
    })
  }

The operator concatMap() waits for the previous inner observable to complete before commencing the next observable, whereas mergeMap() processes multiple inner observables at the same time, with no guarantee of ordering. operator concatMap() 在开始下一个 observable 之前等待前一个内部 observable 完成,而 mergeMap() 同时处理多个内部 observables,不保证顺序。

Try this:试试这个:

const source = from(res.data);
source.pipe(map(({ schedule_id }) => schedule_id))
  .pipe(concatMap(schedule_id => {
    let param = {
      "schedule_id": schedule_id
    }
    console.log("schedule_id#: ", schedule_id);
    return this.fetchApiService.getQuestion(param);
  }))
  .subscribe((result) => {
    console.log('result#:', result);
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM