How do I do something like $q.all
but limiting how many promises are executed concurrently?
My question is just like How can I limit Q promise concurrency?
I want no more than 5 process spawned at a time
The accepted answer for that other question is a library written for promise wrapped to work with Q.
But I'm interested specifically in a solution for Angular's $q
rather than for Q
.
Background: The problem being solved:
I have a bunch of files to download in 2 steps: a) Get URL b) download file.
The browser limits how many files can be retrieved concurrently, so when the straightforward use of promises with $q.all
fires off all the downloads, only N happen right away, e.g. 6 in Chrome, while the rest are delayed. (see Max parallel http connections in a browser?)
Problem is that the URLs have expiry, so by the time the browser executes the N+1th file download, the URL is no longer valid.
So I want to do something like throttled.all(6, promises)
rather than $q.all(promise)