I have been searching and have found some variations of this but have not found anything that meets my needs exactly. Below is the pseudocode that I am trying to figure out:
var itemsUploading = 0;
const maxUploads = 10;
function uploadAll(items){
var promises = [];
items.forEach(function(item){
/* Here is where I want to wait for itemsCurrentlyUploading < maxUploads */
itemsUploading++;
promises.push(upload(item));
}
return Promise.all(promises);
}
function upload(item){
return new Promise(function(resolve, reject){
/*Item upload logic here */
})
.then(function(response){
itemsUploading--;
});
}
I do understand that you cannot block synchronous code to wait for asynchronous and I have tried several variations. I think setTimeout
is something that I can use to do this but I can't quite understand the logic necessary. Any and all help would be greatly appreciated and if there is anything else I can edit this with to help someone understand the problem better just let me know, thank you!