Skip to content

Instantly share code, notes, and snippets.

@crisward
Created July 26, 2016 12:38
Show Gist options
  • Save crisward/7c541376d7a9f4aa40b89d07d64558e2 to your computer and use it in GitHub Desktop.
Save crisward/7c541376d7a9f4aa40b89d07d64558e2 to your computer and use it in GitHub Desktop.
batchFindAll = (model,where,cback,size=10)->
model.count({where})
.then (res)=>
total = res
[0..Math.floor(total/size)].reduce (curr,num)=>
count = num*10
curr.then => cback(where,count,size)
,Promise.resolve()
var batchFindAll = function(model,where,cback,size){
var total = 0;
var offset = 0;
if(size==null) size=10;
return model.count({where:where}).then(function(res){
total = res;
pages = [];
for(var i=0;i<Math.ceil(total/size);i++){
pages.push(i);
}
return pages.reduce (function(curr,num){
var count = num*10;
return curr.then(function(){ return cback(where,count,size);});
},Promise.resolve());
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment