Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed batch exiting later

  • Loading branch information...
commit dcd209025b860b7a821ecd484c0452e0080477c7 1 parent d863e27
Mihai Ene randunel authored
Showing with 10 additions and 2 deletions.
  1. +10 −2 lib/model.js
12 lib/model.js
View
@@ -6,6 +6,7 @@ module.exports = AbstractClass;
/**
* Module dependencies
*/
+var setImmediate = global.setImmediate || process.nextTick;
var util = require('util');
var validations = require('./validations.js');
var ValidationError = validations.ValidationError;
@@ -484,8 +485,15 @@ AbstractClass.iterate = function map(filter, iterator, callback) {
function nextBatch() {
batchNumber += 1;
filter.skip = filter.offset = batchNumber * batchSize;
+ if (limit < batchSize) {
+ filter.limit = Math.abs(limit);
+ }
+ if (filter.limit <= 0) {
+ setImmediate(done);
+ return;
+ }
Model.all(filter, function(err, collection) {
- if (err || collection.length === 0 || limit <= collection.length) {
+ if (err || collection.length === 0 || limit <= 0) {
return done(err);
}
limit -= collection.length;
@@ -510,7 +518,7 @@ AbstractClass.iterate = function map(filter, iterator, callback) {
if (++i >= collection.length) {
return nextBatch();
}
- (global.setImmediate || process.nextTick)(function() {
+ setImmediate(function() {
iterator(collection[i], nextItem, filter.offset + i);
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.