Search code examples
javascriptecmascript-6generatoryield

Clean arrow function - call generator on multilple elements in array


I am trying to use a clean arrow function that calls yield fork(....). I have a lot of places this happens. I am trying to do this:

const { ids } = yield take('REQUEST');

// ids is array of numbers [1, 2, 3]

ids.forEach(id => yield fork(requestWorker, id)); ////// this is what im trying to do - this is pseudo-code i know you cant use in non-generators (non-super-star functions)

However this does not work and i am having to do:

for (const id of ids) {
    yield fork(requestWorker, id);
}

instead of the:

ids.forEach(id => yield fork(requestWorker, id)); // this is pseudo-code i know you cant use in non-generators (non-super-star functions)

Is there a cleaner way then a for-of loop? Like this arrow function method?


Solution

  • You could yield another generator

    function* take(x) {
        var id = /* ... */,
            requesWroker = /* ... */;
    
        yield* fork(requesWroker, id);
    }
    
    var all = [...take('REQUEST')];