I thought generators is more like a funny toy in javascript. They have interesting concept, but almost useless in the real code. It's a surprise for me that this language feature has a real use case in redux-saga!
Yeah true. it is one of the powerful features in javascript. sadly, it is not being used that much. but, redux saga using it in an efficient way possible.
Thanks for the example. The same result might be achieved by iterating the array with the old-school for loop. The array will be iterated only once as well.
This is only possible when you have your conditions when you want to iterate it, imagine series of function passing each other generators, and final evaluator does not have access to all conditions.
Conditions functions should exist somewhere. I think it's always possible to refactor the code and have access to these functions (for example, by moving the conditions functions higher in lexical scope).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I thought generators is more like a funny toy in javascript. They have interesting concept, but almost useless in the real code. It's a surprise for me that this language feature has a real use case in redux-saga!
Yeah true. it is one of the powerful features in javascript. sadly, it is not being used that much. but, redux saga using it in an efficient way possible.
Never needed them so far, but I do use a lot of promises.
Generators are almost identical to enumerables in C#, the real benefit of it is reduce number of iterations. Consider following,
In above example, a is iterated 3 times,
But with generators, you can reduce iteration to single iteration.
If you run this code, you will notice that iterating is only printed once.
You can combine various map/filter or map/reduce to only iterate an array once.
Good example and explanation. :-)
Thanks for the example. The same result might be achieved by iterating the array with the old-school
for
loop. The array will be iterated only once as well.I see that generators help to write code in FP-style and save iteration cycles at the same time.
This is only possible when you have your conditions when you want to iterate it, imagine series of function passing each other generators, and final evaluator does not have access to all conditions.
Conditions functions should exist somewhere. I think it's always possible to refactor the code and have access to these functions (for example, by moving the conditions functions higher in lexical scope).