skip to Main Content

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 .thens, 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


  1. You can either do it manually a tool like p-limit

    Example:

    import pLimit from 'p-limit';
    
    const limit = pLimit(1);
    
    async function getData(x: number) {
      // do whatever you neeed aasynchronously here
      return x;
    }
    
    async function otherFunction() {
      // some input parameters for the async getData function:
      const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    
      // set concurrency limit to 5:
      const limit = pLimit(5);
    
      // run Promite.all on the closures processed by your `limit`
      return Promise.all(input.map(e => limit(() => getData(e))));
    }
    
    Login or Signup to reply.
  2. You can fetch requests sequentially by defining Promise.resolve(), and chaining it every iteration.

    let urls = [];
    Array(21).fill('').forEach((_,i)=>{
      urls.push('https://dummyjson.com/carts/'+(i+1));
    });
    
    let promise = Promise.resolve();
    console.time('time execution');
    urls.forEach((url,i) => {
      promise = promise.then(() => 
        fetch(url)
        .then(r => r.json())
        .then(r => console.log(r))
        .catch(e => console.error(e))
        .finally(() => {
          if(i === urls.length - 1) console.timeEnd('time execution');
        }) 
      );
    });

    Another way: Use async-await

    let urls = [];
    Array(21).fill('').forEach((_,i)=>{
      urls.push('https://dummyjson.com/carts/'+(i+1));
    });
    
    (async () => {
      console.time('time execution')
      for(let [i,url] of urls.entries()) {
        console.log(await new Promise((resolve, reject) => {
          fetch(url)
          .then(r => r.json())
          .then(r => resolve(r))
          .catch(e => reject(e))
          .finally(() => {
            if(i === urls.length - 1) console.timeEnd('time execution');
          });
        }));
      }
    })();
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search