Home > Software design >  Is there any way i can limit this for loop to a maximum of 50 loops?
Is there any way i can limit this for loop to a maximum of 50 loops?

Time:12-21

This is a really unusual code i haven't written myself, each day a database is updated with around 200 records. This means we have to do 200 api calls to figure out infomation about theese records every day at 7 am. This has been working fine until now, the api has implemented a limit of 50 api calls pr hour, and IF you try to do 51 calls, you get banned for 24 hours...

SO, how do i make the for loop do a maximum of 50 loops here?

for (let record of records ) {
    //This loop has to be limited to 50 iterations pr run.
    let brregRawData = await fetch(`some url/${record.name}/someurl`);
    let brregJson = await brregRawData.json()
    let personNavn = brregJson.rollegrupper[0].roller[0].person.navn.fornavn
    let personEtternavn = brregJson.rollegrupper[0].roller[0].person.navn.etternavn
    // if mellomnavn = undefined, then do nothing
    if (brregJson.rollegrupper[0].roller[0].person.navn.mellomnavn == undefined) { 
        var personMellomNavn = ""
    } else {
        let personMellomNavn = brregJson.rollegrupper[0].roller[0].person.navn.mellomnavn   " "
    }

I tried looking up different ways to use a for loop, but all the solutions i looked at broke the "${record.name}" part of the url, i have to loop through an entire view of an entire database.

CodePudding user response:

I'm not sure if this is right for the case, but can you define a counter outside of the loop, increment internally and break when it reaches 50.

let count = 0;
for (....)
   ...
   if (count   > 50) break;
   ...

CodePudding user response:

If I'm understanding you correctly, you need to limit the number of api calls to 50 but the current implementation uses a for ... of loop. The simplest way to achieve what you're looking for with the least amount of modification is to use a standard for loop.

Assuming this all happens inside an async function...

async function limitToFiftyApiCalls(records) {
  const max = Math.min(records.length, 50);
  for (let i = 0; i < max; i  ) {
    const record = records[i];
    let brregRawData = await fetch(`some url/${record.name}/someurl`);
    let brregJson = await brregRawData.json();
    let personNavn = brregJson.rollegrupper[0].roller[0].person.navn.fornavn;
    let personEtternavn = brregJson.rollegrupper[0].roller[0].person.navn.etternavn;
    // if mellomnavn = undefined, then do nothing
    if (brregJson.rollegrupper[0].roller[0].person.navn.mellomnavn == undefined) {
      var personMellomNavn = "";
    } else {
      let personMellomNavn = brregJson.rollegrupper[0].roller[0].person.navn.mellomnavn   " ";
    }
  }
}

The code above doesn't modify your existing code much other than limiting the number of API calls. However there's a few things you could do that would generally make the code easier to read and edit.

async function limitToFiftyApiCalls(records) {
  const max = Math.min(records.length, 50);
  for (let i = 0; i < max; i  ) {
    const record = records[i];
    let personMellomNavn = "";
    let brregRawData = await fetch(`some url/${record.name}/someurl`);
    let brregJson = await brregRawData.json();

    // why write this more than once?
    // save it to a variable and make life easier
    let someVar = brregJson.rollegrupper[0].roller[0].person.navn;
    
    let personNavn = someVar.fornavn;
    let personEtternavn = someVar.etternavn;
    if (someVar.mellomnavn) {
      personMellomNavn = someVar.mellomnavn   ' '
    }
  }
}

CodePudding user response:

Answer to you question:

for (let i=0; i<math.min(records.length, 50); i   ) {
  let record = records[i];

But what happens then with the records you have not checked? Will you wait 24h? I guess that's not what's expected, and instead will have to check 50 records every hour until you have checked them all (so 4 times for 200 records).

CodePudding user response:

A very simple way to do this is implementing a count that increments every time the loop executes the body. So for the the loop you provided it would look like this:

let count = 0; // initialize count as 0

for (let record of records ) {

    if(count >= 50) break; // break out of loop if count is equal to or greater than 50 (since count starts from 0)

    //This loop has to be limited to 50 iterations pr run.
    let brregRawData = await fetch(`some_url/${record.name}/some_url`);
    let brregJson = await brregRawData.json()
    let personNavn = brregJson.rollegrupper[0].roller[0].person.navn.fornavn
    let personEtternavn = brregJson.rollegrupper[0].roller[0].person.navn.etternavn
    
    // if mellomnavn = undefined, then do nothing
    if (brregJson.rollegrupper[0].roller[0].person.navn.mellomnavn == undefined) { 
        var personMellomNavn = ""
    } else {
        let personMellomNavn = brregJson.rollegrupper[0].roller[0].person.navn.mellomnavn   " "
    }

    count  ; // Increment the count after each iteration
}

  • Related