Home > Blockchain >  How do I stop when data comes in nested functions w'th Javascript?
How do I stop when data comes in nested functions w'th Javascript?

Time:12-27

I am trying to fetch news in two date ranges using nested function. The API I wrote brings up to 90 days of data to avoid timeout error. But I want to fetch the data between 1 year. I try to do this by dividing a year into 90 days. I want the for loop to break on the date the data arrives. How do I that ?

My code:

  function requestToDataNews(startDate, endDate) {
    return new Promise((resolve, reject) => {
      http.get(`/test=&start_date=${startDate}&end_date=${endDate}`, { timeout: 40000 }
      )
        .then((response) => {
          if(response.data.response_code === 200){
            resolve(response.data);
          } else {
            reject(response.data);
          }
        })
        .catch((error) => {
          reject(error);
        }).finally(() => {
          commit('loadingBar', false);
        });
    });
 }
 commit('loadingBar', true);
 let startDate = '2022-12-27';
 let endDate = '2021-12-27';
 
 const differentDays = tools.getDifferentDaysRange(endDate, startDate, 'days');
 const currentNewsData = [];
 
 if (differentDays > 90 && differentDays <= 365) {
   for (let i = 0; i < Math.ceil(differentDays / 60); i  = 1){
     startDate = moment(endDate).subtract('months', 2).format('YYYY-MM-DD');
     const newsData = requestToDataNews(startDate, endDate);
     if (newsData.is_success) {
       currentNewsData.push(...newsData.data);
     }
   }
 }
}```

I expect this: if endDate = '2022-12-27' then startDate = '2022-10-27' by calculation. If the response.data is full, I want the loop to break without looking at the past dates. 

CodePudding user response:

Try a nested loops. The outer loop to be 1 to 365 and the inner loop will be 90 days. Once the 90 days is over increment the outer loop by 90 and do the inner loop again.

CodePudding user response:

To achieve this, you can use a break statement within your loop to exit the loop when the condition you want is met. For example, you can add a check to see if the length of the newsData.data array is equal to the maximum number of items that the API allows you to fetch (in your case, 90 or if you want later on 60?). This way, the loop will exit as soon as the API returns the maximum number of items, and it will not continue iterating through the past dates.

Keep in mind that you will also need to update the endDate variable in each iteration of the loop, so that you are always fetching the next 60-day range of data. You can do this by setting endDate to startDate at the end of each iteration:

if (differentDays > 60 && differentDays <= 365) {
  for (let i = 0; i < Math.ceil(differentDays / 60); i  = 1){
    startDate = moment(endDate).subtract('months', 2).format('YYYY-MM-DD');
    const newsData = requestToDataNews(startDate, endDate);
    if (newsData.is_success) {
      currentNewsData.push(...newsData.data);
      if (newsData.data.length === 60) { // or you can make this one 90 if you want to keep it at 90. it's not clear which amount you wanted it. 
        break;
      }
    }
    endDate = startDate;
  }
}

This should allow you to fetch the data in 60-day increments, and stop as soon as you have reached the desired date range.

  • Related