您應該嘗試排隊。我假設當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