lib/internal/querys/stream.js
function _beginBatchMaybe(next) {
  
          // 0   => 15
          // 15  => 15
          // 30  => 15
          // 45  => 5
          // 50
          var numRecordsLeftUntilAbsLimit = query.criteria.limit - ( i*BATCH_SIZE );
          var limitForThisBatch = Math.min(numRecordsLeftUntilAbsLimit, BATCH_SIZE);
          var skipForThisBatch = query.criteria.skip +  ( i*BATCH_SIZE );
          //                     |_initial offset    +  |_relative offset from end of previous batch
  
  
          // If we've exceeded the absolute limit, then we go ahead and stop.
          if (limitForThisBatch <= 0) {
            reachedLastBatch = true;
            return next();
          }//-•
  
          // Build the criteria + deferred object to do a `.find()` for this batch.
          var criteriaForThisBatch = {
            skip: skipForThisBatch,
            limit: limitForThisBatch,
            sort: query.criteria.sort,
            select: query.criteria.select,
            omit: query.criteria.omit,
            where: query.criteria.where
          };
          // console.log('---iterating---');
          // console.log('i:',i);
          // console.log('   BATCH_SIZE:',BATCH_SIZE);
          // console.log('   query.criteria.limit:',query.criteria.limit);
          // console.log('   query.criteria.skip:',query.criteria.skip);
          // console.log('   query.criteria.sort:',query.criteria.sort);
          // console.log('   query.criteria.where:',query.criteria.where);
          // console.log('   query.criteria.select:',query.criteria.select);
          // console.log('   query.criteria.omit:',query.criteria.omit);
          // console.log('   --');
          // console.log('   criteriaForThisBatch.limit:',criteriaForThisBatch.limit);
          // console.log('   criteriaForThisBatch.skip:',criteriaForThisBatch.skip);
          // console.log('   criteriaForThisBatch.sort:',criteriaForThisBatch.sort);
          // console.log('   criteriaForThisBatch.where:',criteriaForThisBatch.where);
          // console.log('   criteriaForThisBatch.select:',criteriaForThisBatch.select);
          // console.log('   criteriaForThisBatch.omit:',criteriaForThisBatch.omit);
          // console.log('---•••••••••---');
          var deferredForThisBatch = WLModel.find(criteriaForThisBatch);
  
          _.each(query.populates, function (assocCriteria, assocName){
            deferredForThisBatch = deferredForThisBatch.populate(assocName, assocCriteria);
          });
  
          // Pass through `meta` so we're sure to use the same db connection
          // and settings (i.e. esp. relevant if we happen to be inside a transaction)
          deferredForThisBatch.meta(query.meta);
  
          deferredForThisBatch.exec(function (err, batchOfRecords){
            if (err) { return next(err); }
  
            // If there were no records returned, then we have already reached the last batch of results.
            // (i.e. it was the previous batch-- since this batch was empty)
            // In this case, we'll set the `reachedLastBatch` flag and trigger our callback,
            // allowing `async.whilst()` to call _its_ callback, which will pass control back
            // to userland.
            if (batchOfRecords.length === 0) {
              reachedLastBatch = true;
              return next();
            }// --•
  
            // But otherwise, we need to go ahead and call the appropriate
            // iteratee for this batch.  If it's eachBatchFn, we'll call it
            // once.  If it's eachRecordFn, we'll call it once per record.
            (function _makeCallOrCallsToAppropriateIteratee(proceed){
  
              // If an `eachBatchFn` iteratee was provided, we'll call it.
              // > At this point we already know it's a function, because
              // > we validated usage at the very beginning.
              if (query.eachBatchFn) {
  
                // Note that, if you try to call next() more than once in the iteratee, Waterline
                // logs a warning explaining what's up, ignoring all subsequent calls to next()
                // that occur after the first.
                var didIterateeAlreadyHalt;
                try {
                  var promiseMaybe = query.eachBatchFn(batchOfRecords, function (err) {
                    if (err) { return proceed(err); }
  
                    if (didIterateeAlreadyHalt) {
                      console.warn(
                        'Warning: The per-batch iteratee provided to `.stream()` triggered its callback \n'+
                        'again-- after already triggering it once!  Please carefully check your iteratee\'s \n'+
                        'code to figure out why this is happening.  (Ignoring this subsequent invocation...)'
                      );
                      return;
                    }//-•
  
                    didIterateeAlreadyHalt = true;
  
                    return proceed();
                  });//_∏_  </ invoked per-batch iteratee >
  
                  // Take care of unhandled promise rejections from `await`.
                  if (query.eachBatchFn.constructor.name === 'AsyncFunction') {
                    promiseMaybe.catch(function(e){ proceed(e); });//_∏_
                  }
  
                } catch (e) { return proceed(e); }//>-•
  
                return;
              }//_∏_.
  
  
              // Otherwise `eachRecordFn` iteratee must have been provided.
              // We'll call it once per record in this batch.
              // > We validated usage at the very beginning, so we know that
              // > one or the other iteratee must have been provided as a
              // > valid function if we made it here.
              async.eachSeries(batchOfRecords, function _eachRecordInBatch(record, next) {
                // Note that, if you try to call next() more than once in the iteratee, Waterline
                // logs a warning explaining what's up, ignoring all subsequent calls to next()
                // that occur after the first.
                var didIterateeAlreadyHalt;
                try {
                  var promiseMaybe = query.eachRecordFn(record, function (err) {
                    if (err) { return next(err); }
  
                    if (didIterateeAlreadyHalt) {
                      console.warn(
                        'Warning: The per-record iteratee provided to `.stream()` triggered its callback\n'+
                        'again-- after already triggering it once!  Please carefully check your iteratee\'s\n'+
                        'code to figure out why this is happening.  (Ignoring this subsequent invocation...)'
                      );
                      return;
                    }//-•
  
                    didIterateeAlreadyHalt = true;
  
                    return next();
  
                  });//_∏_  </ invoked per-record iteratee >
  
                  // Take care of unhandled promise rejections from `await`.
                  if (query.eachRecordFn.constructor.name === 'AsyncFunction') {
                    promiseMaybe.catch(function(e){ next(e); });//_∏_
                  }
                } catch (e) { return next(e); }
  
              },// ~∞%°
              function _afterIteratingOverRecordsInBatch(err) {
                if (err) { return proceed(err); }
  
                return proceed();
  
              });//</async.eachSeries()>
  
            })(function _afterCallingIteratee(err){
              if (err) {
                return next(err);
              }
  
              // Increment the batch counter.
              i++;
  
              // On to the next batch!
              return next();
  
            });//</self-calling function :: process this batch by making either one call or multiple calls to the appropriate iteratee>
  
          });//</deferredForThisBatch.exec()>
  
        }
function _beginBatchMaybe(next) {
  
          // 0   => 15
          // 15  => 15
          // 30  => 15
          // 45  => 5
          // 50
          var numRecordsLeftUntilAbsLimit = query.criteria.limit - ( i*BATCH_SIZE );
          var limitForThisBatch = Math.min(numRecordsLeftUntilAbsLimit, BATCH_SIZE);
          var skipForThisBatch = query.criteria.skip +  ( i*BATCH_SIZE );
          //                     |_initial offset    +  |_relative offset from end of previous batch
  
  
          // If we've exceeded the absolute limit, then we go ahead and stop.
          if (limitForThisBatch <= 0) {
            reachedLastBatch = true;
            return next();
          }//-•
  
          // Build the criteria + deferred object to do a `.find()` for this batch.
          var criteriaForThisBatch = {
            skip: skipForThisBatch,
            limit: limitForThisBatch,
            sort: query.criteria.sort,
            select: query.criteria.select,
            omit: query.criteria.omit,
            where: query.criteria.where
          };
          // console.log('---iterating---');
          // console.log('i:',i);
          // console.log('   BATCH_SIZE:',BATCH_SIZE);
          // console.log('   query.criteria.limit:',query.criteria.limit);
          // console.log('   query.criteria.skip:',query.criteria.skip);
          // console.log('   query.criteria.sort:',query.criteria.sort);
          // console.log('   query.criteria.where:',query.criteria.where);
          // console.log('   query.criteria.select:',query.criteria.select);
          // console.log('   query.criteria.omit:',query.criteria.omit);
          // console.log('   --');
          // console.log('   criteriaForThisBatch.limit:',criteriaForThisBatch.limit);
          // console.log('   criteriaForThisBatch.skip:',criteriaForThisBatch.skip);
          // console.log('   criteriaForThisBatch.sort:',criteriaForThisBatch.sort);
          // console.log('   criteriaForThisBatch.where:',criteriaForThisBatch.where);
          // console.log('   criteriaForThisBatch.select:',criteriaForThisBatch.select);
          // console.log('   criteriaForThisBatch.omit:',criteriaForThisBatch.omit);
          // console.log('---•••••••••---');
          var deferredForThisBatch = WLModel.find(criteriaForThisBatch);
  
          _.each(query.populates, function (assocCriteria, assocName){
            deferredForThisBatch = deferredForThisBatch.populate(assocName, assocCriteria);
          });
  
          // Pass through `meta` so we're sure to use the same db connection
          // and settings (i.e. esp. relevant if we happen to be inside a transaction)
          deferredForThisBatch.meta(query.meta);
  
          deferredForThisBatch.exec(function (err, batchOfRecords){
            if (err) { return next(err); }
  
            // If there were no records returned, then we have already reached the last batch of results.
            // (i.e. it was the previous batch-- since this batch was empty)
            // In this case, we'll set the `reachedLastBatch` flag and trigger our callback,
            // allowing `async.whilst()` to call _its_ callback, which will pass control back
            // to userland.
            if (batchOfRecords.length === 0) {
              reachedLastBatch = true;
              return next();
            }// --•
  
            // But otherwise, we need to go ahead and call the appropriate
            // iteratee for this batch.  If it's eachBatchFn, we'll call it
            // once.  If it's eachRecordFn, we'll call it once per record.
            (function _makeCallOrCallsToAppropriateIteratee(proceed){
  
              // If an `eachBatchFn` iteratee was provided, we'll call it.
              // > At this point we already know it's a function, because
              // > we validated usage at the very beginning.
              if (query.eachBatchFn) {
  
                // Note that, if you try to call next() more than once in the iteratee, Waterline
                // logs a warning explaining what's up, ignoring all subsequent calls to next()
                // that occur after the first.
                var didIterateeAlreadyHalt;
                try {
                  var promiseMaybe = query.eachBatchFn(batchOfRecords, function (err) {
                    if (err) { return proceed(err); }
  
                    if (didIterateeAlreadyHalt) {
                      console.warn(
                        'Warning: The per-batch iteratee provided to `.stream()` triggered its callback \n'+
                        'again-- after already triggering it once!  Please carefully check your iteratee\'s \n'+
                        'code to figure out why this is happening.  (Ignoring this subsequent invocation...)'
                      );
                      return;
                    }//-•
  
                    didIterateeAlreadyHalt = true;
  
                    return proceed();
                  });//_∏_  </ invoked per-batch iteratee >
  
                  // Take care of unhandled promise rejections from `await`.
                  if (query.eachBatchFn.constructor.name === 'AsyncFunction') {
                    promiseMaybe.catch(function(e){ proceed(e); });//_∏_
                  }
  
                } catch (e) { return proceed(e); }//>-•
  
                return;
              }//_∏_.
  
  
              // Otherwise `eachRecordFn` iteratee must have been provided.
              // We'll call it once per record in this batch.
              // > We validated usage at the very beginning, so we know that
              // > one or the other iteratee must have been provided as a
              // > valid function if we made it here.
              async.eachSeries(batchOfRecords, function _eachRecordInBatch(record, next) {
                // Note that, if you try to call next() more than once in the iteratee, Waterline
                // logs a warning explaining what's up, ignoring all subsequent calls to next()
                // that occur after the first.
                var didIterateeAlreadyHalt;
                try {
                  var promiseMaybe = query.eachRecordFn(record, function (err) {
                    if (err) { return next(err); }
  
                    if (didIterateeAlreadyHalt) {
                      console.warn(
                        'Warning: The per-record iteratee provided to `.stream()` triggered its callback\n'+
                        'again-- after already triggering it once!  Please carefully check your iteratee\'s\n'+
                        'code to figure out why this is happening.  (Ignoring this subsequent invocation...)'
                      );
                      return;
                    }//-•
  
                    didIterateeAlreadyHalt = true;
  
                    return next();
  
                  });//_∏_  </ invoked per-record iteratee >
  
                  // Take care of unhandled promise rejections from `await`.
                  if (query.eachRecordFn.constructor.name === 'AsyncFunction') {
                    promiseMaybe.catch(function(e){ next(e); });//_∏_
                  }
                } catch (e) { return next(e); }
  
              },// ~∞%°
              function _afterIteratingOverRecordsInBatch(err) {
                if (err) { return proceed(err); }
  
                return proceed();
  
              });//</async.eachSeries()>
  
            })(function _afterCallingIteratee(err){
              if (err) {
                return next(err);
              }
  
              // Increment the batch counter.
              i++;
  
              // On to the next batch!
              return next();
  
            });//</self-calling function :: process this batch by making either one call or multiple calls to the appropriate iteratee>
  
          });//</deferredForThisBatch.exec()>
  
        }
~∞%°