async icon indicating copy to clipboard operation
async copied to clipboard

async.each with empty MongooseArray never calls final callback

Open RBLU opened this issue 11 years ago • 4 comments

We just tried to update async, mongoose and io.js and noticed a problem with async.forEach hanging when we pass a Mongoose Array of length 0. The following code leads to the problems:

lib/async.js:224

    var size = object.length || _keys(object).length;

the object.length is 0 because we have an empty Array, and 0 evaluates to falsy, but then mongoose attaches properties to the Array Object, so _keys(object).length returns 33, so size ends up being 33.

but then on iteration in _each the function isArrayLike returns true so it uses _arrayEach to iterate, but there are no entries to iterate because object.length is 0, so the final callback is never called, the 33 properties are never iterated.

This seems inconsistent. I think a correct fix would be to use the isArrayLike in both places:

lib/async.js:224

    var size = isArrayLike(object) ? object.length : _keys(object).length;

RBLU avatar Jun 02 '15 15:06 RBLU

Good catch. A PR with a regression test would be welcome! Otherwise I'll get to this later today.

aearly avatar Jun 02 '15 16:06 aearly

ok, will submit PR

RBLU avatar Jun 02 '15 18:06 RBLU

PR see #777

RBLU avatar Jun 02 '15 18:06 RBLU

Thanks for your help!

aearly avatar Jun 02 '15 20:06 aearly