I've been working (and searching) on finding a solution for this for a few days, and while the following works, I can't help but feel there's a more rxjs-y way of doing this.
I have an array of URLs to download from an external source which has rate limiting, no more than say 100 calls per minute.
const sources = ['a.jpg', 'b.jpg', 'c.jpg'];
timer(0, 1000).pipe(
switchMap(index => of(sources[index])),
takeWhile(_ => _ !== undefined),
switchMap(url => {
return from(download(url))
})
).subscribe(
next => console.log(next),
err => console.error(err),
() => console.info('Done')
)
// Pseudo
function download(url) {
return new Promise((resolve, reject) => {
resolve('Downloaded ' + url)
})
}
Seems a bit roundabout and hacky.
What's the best way to iterate through an array to not get locked out for too many request/second?