您应该尝试排队。我假设当upload_file()
完成时会触发回调。这样的事情应该做的伎俩(未经测试):
function upload_files(files, maxSimultaneousUploads, callback) {
var runningUploads = 0,
startedUploads = 0,
finishedUploads = 0;
function next() {
runningUploads--;
finishedUploads++;
if (finishedUploads == files.length) {
callback();
} else {
// Make sure that we are running at the maximum capacity.
queue();
}
}
function queue() {
// Run as many uploads as possible while not exceeding the given limit.
while (startedUploads < files.length && runningUploads < maxSimultaneousUploads) {
runningUploads++;
upload_file(files[startedUploads++], next);
}
}
// Start the upload!
queue();
}
类似但速率限制(每秒/分钟)是在这里:https://stackoverflow.com/questions/20253425/throttle-and-queue-up-api-requests-due-to-per- second-cap – 2017-12-04 06:33:32