I've got a rather complex function that describes an iterative process. It goes something like this (I have lots of code not relevant to the question):
function* functionName(
config: Config,
poolSize: number
): Generator<[State, Step], boolean, void> {
/* ... */
for (const step of someOtherProcess()) {
/* ... */
yield [state, step]
switch (stateType) {
case StateType.Start:
if (/* ... */) {
return false
} else {
return true
}
case StateType.Invalid:
return false
}
}
return false
}
It basically does three things: (1) simulates a certain process, (2) provides details of each step of the process (as you can see with yield
), and (3) gives a final result when the process is finished. Naturally, tasks (3) and (2) are inherent to (1), hence why they're handled in one function.
Nevertheless, while some users of the function want all steps (2), others only care about the end outcome (3).
Currently, I'm using this approach:
const generator = functionName(config, poolSize)
let result
while (!(result = generator.next()).done) {}
return result.value as boolean
Is there a more conventional and simpler way to achieve the same outcome?