Replies: 2 comments 1 reply
-
I found one way of creating the const delay = (ms: number) => new Promise(res => setTimeout(res, ms))
const timer = (ms: number) => Rune.asyncIter(() => ({
async *[Symbol.asyncIterator]() {
let idx = 0
while (true) {
yield idx++
await delay(ms)
}
},
})) However, this is very problematic, because it's impossible for the consumer to stop the async-iterator... For instance, if you try running this code (which is based on the code provided on the Comparison with RxJS example) : const delay = (ms: number) =>
new Promise<void>((res) =>
setTimeout(() => {
console.log("done delaying!")
res()
}, ms),
)
const timer = (ms: number) => Rune.asyncIter(() => ({
async *[Symbol.asyncIterator]() {
let idx = 0
while (true) {
yield idx++
await delay(ms)
}
},
}))
const a = timer(1000).map((n) => `a${n}`)
const b = timer(1500).map((n) => `b${n}`)
const ab = Rune.tuple([a, b])
const start = Date.now()
let idx = 0
for await (const value of ab.iter()) {
console.log(Date.now() - start, value)
if (++idx === 5) {
console.log("stop everything!!")
break
}
} then you will notice that after the So, my question is: is there something equivalent to |
Beta Was this translation helpful? Give feedback.
-
I'm merging all of the conversation into this discussion. |
Beta Was this translation helpful? Give feedback.
-
Some of the things that are explained in this doc don't feel right/intuitive to me, so I wanted to test those examples on my own.
However, I couldn't find the
timer
function that it's used in the example for producing the Rune that emits timely events anywhere within CAPI.Which makes me wonder: how can a user (or a library author who wants to support Runes) implement a function like that?
Also: how can the consumer of that event-emitter Rune instruct it to stop emitting values? In other words: what are the semantics for canceling an ongoing Rune?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions