Skip to content

Instantly share code, notes, and snippets.

@cyjake
Created May 27, 2014 14:36
Show Gist options
  • Save cyjake/c574e865b519e35b9cd4 to your computer and use it in GitHub Desktop.
Save cyjake/c574e865b519e35b9cd4 to your computer and use it in GitHub Desktop.
// 基于 Promise 的分批函数
// 遇到文件句柄不够用、并发请求数限制等场景的时候挺方便的
function batch(arr, size, fn) {
var offset = 0
var length = arr.length
var limit = size
function _batch(offset, limit) {
return Promise.all(arr.slice(offset, limit).map(fn)).then(function() {
if (limit <= length) {
offset = limit
limit += size
return _batch(offset, limit)
}
})
}
return _batch(offset, limit)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment