如何延迟分组的结果

问题描述 投票:0回答:2

我如何一次获得单个项目(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);
rxjs rxjs5 rxjs6
2个回答
0
投票

您可以使用setTimeout命令安排操作

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

或取消订阅:

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

或者再次订阅:

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

或延迟console.log:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.