如何延迟分组结果

时间:2019-04-27 01:55:58

标签: rxjs rxjs5 rxjs6

我如何一次获得单个项目(concatMerge)并从group.pipe(toArray())的结果中延迟它。我希望每个项目都从组数组中出来,并在该项目组内部延迟。

const people = [
  { name: 'Sue', age: 25 },
  { name: 'Joe', age: 30 },
  { name: 'Frank', age: 25 },
  { name: 'Sarah', age: 35 }
];

from(people).pipe(
  groupBy(person => person.age, p => p.name),
  mergeMap(group => zip(of(group.key), group.pipe(toArray())))
).subscribe(console.log);

2 个答案:

答案 0 :(得分:0)

您可以使用return <WrappedComponent {...this.props} />命令安排操作

setTimeout

或取消订阅:

setTimeout (() => {
     console.log("Hello from setTimeout");
  }, 1000);

或再次订阅:

// Stop listening for location after 10 seconds
setTimeout(() => { locationsSubscription.unsubscribe(); }, 10000);

或延迟console.log:

// After 1/2 second, subscribe again.
setTimeout(() => {
  sequence.subscribe({
    next(num) { console.log('2nd subscribe: ' + num); },
    complete() { console.log('2nd sequence finished.'); }
  });
}, 500);

答案 1 :(得分:0)

使用`delay()

from(people).pipe(
  groupBy(person => person.age, p => p.name),
  concatMap(group => zip(of(group.key), group.pipe(toArray())).pipe(delay(2000)))
).subscribe(console.log);

https://stackblitz.com/edit/rxjs-groupby-key-vals-s8nsjq?file=index.ts