-1

What does the following syntax mean? I don’t understand the use of a * after a yield. I’m new to generators, redux, and sagas, so I would appreciate some help understanding what the syntax *, takeEvery(), and the return function *(action) { do:

var MIDDLEWARES = []

function builder( ) {
    const LOAD_DATA = "POI_LOADER/LOAD_POIS"
    MIDDLEWARES.push( function *sagaFunction() {
        yield *takeEvery( LOAD_DATA, loadData( statusField) )
    } )
}

const loadData = (statusField) => {
    return function *(action) {
        console.log("action.venueId = " + action.venueId)
    }
}

There are several questions here:

  1. What does the * in yield *takeEvery() mean? Seems to be answered by Delegated yield (yield star, yield *) in generator functions.
  2. Why doesn't the builder() function need a * to make it a generator given that it contains a yield * statement? Is that because the yield *takeEvery() is wrapped in the generator function sagaFunction()?
  3. What does the takeEvery() function do, especially given that it has a * in front of it? I think takeEvery(). Based on its documentation, I think it applies loadData() to everything in LOAD_DATA. But if LOAD_DATA isn't an array, is takeEvery() needed in this code?
  4. How come the declaration return function *(action) seems to have no name for the function? Is it declaring a generator with input parameter action and assigning that generator for a const called loadData?
  5. Does the Saga library call next() on the generators created in this code?
Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
  • Is that legal? It looks strange. The `*` is part of the keyword, i.e. it is `function*` and `yield*`. I didn't know that whitespace is allowed, that's crazy. – Jörg W Mittag Jun 16 '17 at 23:43
  • [clicky](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield*) – Cypher Jun 16 '17 at 23:56

1 Answers1

1

So generators allow to return a value from a generator function by using yield and then resume execution from there on the next call.

yield* is used to indicate that the value returned is coming from another generator - so the generator function that calls yield* is delegating the actual value creation to another generator function in this case.

See MDN on this for more info.

geekonaut
  • 5,714
  • 2
  • 28
  • 30