What's the most elegant way (in Calmm stack, Kefir, Karet Utils etc.) of doing repeated (infinite) async jobs with interval?
I want to get
some json every 2000ms
.
Example 1 (2000ms interval):
get
takes 100ms)Example 2 (2000ms interval)
get
takes 5000ms)So in short, I want to to repeated get
s (or any async work) and wait a minimum of 2000ms
between requests.
I don't want the next request to fire until the previous one has finished in some way (success, fail, timeout).
答案 0 :(得分:1)
这就是我如何使用Kefir而不诉诸Kefir.stream
。请注意,下面的job
将是您在每个刻度上执行的任务。我创建了一个虚拟任务,让代码工作。
let result = Kefir.repeat(() => {
let job = Kefir.later(Math.random() * 5000);
let wait = Kefir.later(2000).ignoreValues();
return Kefir.merge([job, wait]);
});
下面是使用Kefir文档中的样式显示事件:
spawned 1 - job: ------1X
spawned 1 - wait: -----------X
spawned 1 - merge: ------1----X
spawned 2 - job: ------------------2X
spawned 2 - wait: -----------X
spawned 2 - merge: ------------------2X
spawned 3 - job: ---3X
spawned 3 - wait: -----------X
spawned 3 - merge: ---3-------X
result: ------1-----------------------2----3--------...