cujojs/when

when.iterate bug

ericmarkmartin opened this issue · 2 comments

According to the docs, the seed value passed to when.iterate is passed to the handler and then f. However, it is first passed to the predicate.

Hey @ericmarkmartin, this seems like just unclear documentation rather than a bug in functionality. Thanks for reporting it.

@briancavalier: I think the documentation is incorrect, rather than just unclear.

It gives this example:

// Logs
// 0
// 1
// 2
// ...
// 100000000000
when.iterate(function(x) {
    return x+1;
}, function(x) {
    // Stop when x >= 100000000000
    return x >= 100000000000;
}, function(x) {
    console.log(x);
}, 0).done();

However if you run this example, the output stops one item short of expected, eg: if the target was 5 you would get:

0
1
2
3
4

This occurs because the predicate is evaluated before the process function, and the process function is not called for the final value passed to the predicate function. ie: the value that the predicate returns true for.

I'm not sure which was the originally intended behaviour, but it would be great if one of the two was altered to make things consistent.