Am not able to iterate through the data that I am getting. Tried forEach.., for..of and map()

Able to log the data(array of objects) to the console without iterating. I understand the data that I am fetching in a for loop might not have finished executing all the get requests by the time I am using allOrders later but I am just not able to find a solution for this. Thanks for your help.

   
const pageCount = Math.ceil((sortBy === 'deliveryInfo.slotStart' ? countTotalDeliveryDatePageResp : pageResponse) / 20);

// for excel export
let allOrders = [];

let promises = [];

      for(let pageNum = 0; pageNum < pageCount; pageNum++){

        let excelOrderAPI = sortBy === 'deliveryInfo.slotStart' ? "orders/delivery/"+region_id+"/"+endDeliveryDate+"?page="+pageNum+"&size=20" : "/orders/details/?page="+pageNum+"&placedDate.specified=true&placedDate.greaterThanOrEqual="+startDate+"&placedDate.lessThanOrEqual="+endDate+"&size=20&sort="+sortBy+",desc&status.in="+selected.join(',')+"";
        
        promises.push(

          axios.get(excelOrderAPI, {
                    headers: {
                    'Authorization': jwtToken,
                    'Accept' : '*/*',
                    'Content-Type': 'application/json',
                    'App-Token' : 'A14BC'
                      }
            })
            .then(order =>{
  
              if(order.data != null && order.data.length > 0){
                    order.data.map(res=>{
                          allOrders.push(res)
                      })
                
                }
 
           })

        )//end of push()
        

      }


   Promise.all(promises);

Answer

Try with this async/await style code. I find it much more readable than promise chaining.

const mainFunction = async () => {
   // for excel export
   let allOrders = [];
   const pageNum = 0;
   while (pageNum < pageCount) {
     let excelOrderAPI =
       sortBy === "deliveryInfo.slotStart"
         ? "orders/delivery/" +
           region_id +
           "/" +
           endDeliveryDate +
           "?page=" +
           pageNum +
           "&size=20"
         : "/orders/details/?page=" +
           pageNum +
           "&placedDate.specified=true&placedDate.greaterThanOrEqual=" +
           startDate +
           "&placedDate.lessThanOrEqual=" +
           endDate +
           "&size=20&sort=" +
           sortBy +
           ",desc&status.in=" +
           selected.join(",") +
           "";
 
     const order = await axios.get(excelOrderAPI, {
       headers: {
         Authorization: jwtToken,
         Accept: "*/*",
         "Content-Type": "application/json",
         "App-Token": "A14BC",
       },
     });
 
     if (order.data != null && order.data.length > 0) {
       order.data.map((res) => {
         allOrders.push(res);
       });
     }
     pageNum++;
   }

    return allOrders;
 };

const allOrdersData = mainFunction(); allOrdersData.then(function(result){ 
                result && result.map(res=>{// do something 
                with the result}); 
             });