【问题标题】:how can i throttle an array into a stream created using rxjs "from" operator?如何将数组限制为使用 rxjs“from”运算符创建的流?
【发布时间】:2020-07-27 12:18:50
【问题描述】:

我想限制使用“from”运算符创建的数组 observable 的内容,以显示每个值相隔 1 秒。按预期省略油门显示 1 到 10。如果我在可观察对象的 pipe() 中引入节流阀,则流仅输出“1”。

https://stackblitz.com/edit/rxjs-behaviorsubject-simpleexample-knappa?file=index.ts

// RxJS v6+
import { from, interval } from 'rxjs';
import { throttle } from 'rxjs/operators';

const observable = from([1,2,3,4,5,6,7,8,9,10]);
const example = observable.pipe(
  // removing this line displays the full array all at once
  throttle(ev => interval(1000))
  // what we want is 1..2..3..4.. printed in succession, 1 number a second
);
example.subscribe(x => console.log(x));

如何打印相隔 1 秒的数组的数字?

通过扩展,我如何将数字添加到使用“from”创建的流中并继续间隔 1 秒限制这些值?

这很不直观。任何帮助将不胜感激。提前致谢。

【问题讨论】:

标签: arrays rxjs throttling


【解决方案1】:

我不得不谷歌,但这应该给你你想要的:

// RxJS v6+
import { of, from } from 'rxjs';
import { delay, concatMap } from 'rxjs/operators';

const observable = from([1,2,3,4,5,6,7,8,9,10]);
const example = observable.pipe(
  // removing this line displays the full array all at once
  concatMap(item => of(item).pipe(delay(1000)))
  // what we want is 1..2..3..4.. printed in succession, 1 number a second
);
example.subscribe(x => console.log(x));

https://stackblitz.com/edit/rxjs-delay-from-array

【讨论】:

  • 感谢您的回答达到了最终结果,但并未说明如何使用节流阀来减慢数组或其他值源的发射。我需要了解为什么油门不能作为了解如何限制其他事件的值的中间步骤。
  • 好的,我稍后再试试throttle
  • 嘿,谢谢,这不是一个请求,但我真的很感谢您的帮助和洞察力。
  • 没问题,我不认为throttle 是您想要的,因为throttle 会忽略在该时间范围内发出的可观察数据。我试着摆弄它,我做不到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多