cujojs / when

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

`when.any` behavior with empty array #330

Closed benjamingr closed 10 years ago

benjamingr commented 10 years ago

Currently, with the following code both when.any([]) and when.any([resolvesToUndefinedFirst,...]) behave exactly the same way.

I'm wondering why that is and what the rationale about it is. At first glance it seems to me that when.any([]) (and similarly when.some) should throw a range error.

(Similarly, when calling when.any without an array argument).

Thanks!

Related: https://github.com/petkaantonov/bluebird/issues/233

when.any([]).then(function(val){
    document.body.innerHTML = JSON.stringify(val); 
});

var p = when.resolve(undefined);
when.any([p]).then(function(val){
    document.body.innerHTML += " "+ JSON.stringify(val); 
});
// ends up with body being `undefined undefined`

Online example: http://jsfiddle.net/3RAk8/

briancavalier commented 10 years ago

@benjamingr Yeah, it seems pretty reasonable to reject for any kind of N-winner race where it's known up front that there simply aren't enough inputs to ever fulfill it. That jives with the notion that at the point where it's known that the race can't be fulfilled, it rejects. I think we considered doing that for 3.0, but it fell off the radar.

Using a RangeError as suggested in petkaantonov/bluebird#233 seems pretty reasonable--better than returning never() like race() has to do :(

@unscriptable and @scothis, this would be a breaking change for 3.x, but honestly, I think I'd rather just call it a bug and fix it in 3.3 than wait for 4.0 (when.some is already deprecated anyway). Any thoughts or objections?

unscriptable commented 10 years ago

I know we'll likely break a few users' code, but this does feel like a bug.

:+1:

briancavalier commented 10 years ago

Fixed by #332. Thanks again @benjamingr!