Reputation: 565
I am working on a project and there is some refactor to do. For internal decision we do not want to use generators and I came across this code (which it looks weird to me because it seems that there is no need for a generator at all). How would I go to convert it to a normal function (I don't think there is any async operation as far as I can tell)?
Just to make clear I do not want to use generators in this code.
Code:
const getResults = (totalData) => function* getNext() {
const combinations = totalData.reduce((a, b) => a * b.length, 1)
for (let i = 0; i < combinations; i++) {
yield createSolution(i, totalData)
}
return null
}
This is how is being called:
const result = getResults(obj.elementsInObj);
for (let data of result()) {
const resolve = validateData(data, obj.elementsInObj)
if (resolve) {
return resolve
}
}
Upvotes: 1
Views: 433
Reputation: 33726
Well, you can remove the asterisk and yield
operator and create an internal array to store the solutions
, then you can return that array and loop over it.
const getResults = (totalData) => {
const combinations = totalData.reduce((a, b) => a * b.length, 1),
arr = [];
for (let i = 0; i < combinations; i++) arr.push(createSolution(i, totalData));
return arr;
}
const results = getResults(obj.elementsInObj);
for (let data of results) {
const resolve = validateData(data, obj.elementsInObj)
if (resolve) return resolve
}
Upvotes: 4