Let’s say I request a url, which returns the max number of pages, ie 5. I then need to request page 2, 3, 4 and 5.
However, as to not get blocked, these can’t be done all at the same time but rather sequentially so Promise.all()
is not gonna work (if I understand it correctly).
If I knew how many pages I have to get beforehand, I could just chain .then
s, but I only get the info after the first request.
Also the data from all the requests have to be gathered, processed and passed to another function.
I suspect it will have something to do with promises? But how to chain those properly is beyond me.
2
Answers
You can either do it manually a tool like
p-limit
Example:
You can fetch requests sequentially by defining
Promise.resolve()
, and chaining it every iteration.Another way: Use async-await