Reputation: 795
I'm trying to make my generator more flexible
From this:
function *formValidationSequence () {
yield someRef.current.func()
yield someOtherRef.current.func()
...
}
To this:
const array = [someRef, someOtherRef]
const handler = (array) => {
const sequence = testSequence(function *() {
yield* array.map(item => item.current.func())
})
}
But it doesn't work. I want to create yield
dynamically for every ref/item in an array, without to call 'em instantly.
How it should be done?
Upvotes: 2
Views: 1228
Reputation: 138267
You do
yield* iterable
which will create an iterator from the iterable and yield all it's values up. Now your iterable is the thing returned from .map
, which is an array that already contains all the calculated results. To yield
every item one after another, you need a for
loop, as that can be interrupted:
for(const item of [someRef, someOtherRef])
yield* item.current.func();
Upvotes: 3
Reputation: 35512
map
will execute the function on every item in the array before calling yield*
on it. If you want to only call one function per iteration, then just use a regular loop:
function*() {
for (const el of array) {
yield el.current.func();
}
}
Upvotes: 1