cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 396 forks source link

when.iterate bug #462

Open ericmarkmartin opened 9 years ago

ericmarkmartin commented 9 years ago

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.

briancavalier commented 9 years ago

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

mnahkies commented 9 years ago

@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.