Created
May 27, 2014 14:36
-
-
Save cyjake/c574e865b519e35b9cd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 基于 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