How can I delay a javascript promise function to finish the process of sending batches of json

Viewed 31

I have a function that processes 200,000 excel rows and converts them into Json, my problem comes when in the following function I send the Json to PHP in batches, all the batches are sent at the same time and what I need to do is receive one by one to give PHP time to process the batch that arrives first and so on. In the ProcessjsonBud(dt1) function I process the batches in an iterator that passes the json in a jsonaux variable which in turn enters an if conditional where I indicate that the batches cannot be greater than 15000 rows, but in the fetch I get it returns everything at the same time and sometimes one of the batches is truncated with a 500 error, how can I pass batch by batch and give it time to finish processing each one in cascade? This first one, gets the excel file and convert it into json (is working properly)

let selectedFile;

document.getElementById('importBUDid').addEventListener('change', (event)=>{
    selectedFile = event.target.files[0];
});

document.getElementById('uploadBud').addEventListener('click', ()=>{
    let jsonObj;
    if (selectedFile) {
        let fileReader = new FileReader();
        fileReader.readAsBinaryString(selectedFile);
        fileReader.onload = (event)=>{
            //console.log(event.target.result);
            let dato = event.target.result;
            let workbook = XLSX.read(dato, {type:"binary"});
            //console.log(workbook.Strings);
            workbook.SheetNames.forEach(sheet => {
                let rowObject = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheet]);
                
                //console.log(rowObject);
                jsonObj = rowObject;
            });
            //console.log(jsonObj);
            ProcessjsonBud(jsonObj);
            alert('Ready');
        }
    }
}); 
This second one is the one that process the json to send it to PHP, but it is not passing one by one, here is the problem, it sends all the batches at the same time

function ProcessjsonBud(dt1){
    let d= new Date();
    let TxtName = [d.getFullYear(),
                d.getMonth()+1,
               d.getDate()].join('_')+'_'+
              [d.getHours(),
               d.getMinutes(),
               d.getSeconds()].join('_');
               
    let Flagfiletxt=true; //esta se usa para enviar com parametr, en el primer lote enviado se envia como true para que genere el txt
    let contador=0;
    let filasrecorridas=0;
    let jsonaux=[];

    let rowsavailables=dt1.length;
    
    //console.log(dt1);
    
    //con un for crear un objeto json con 15,000 items
    //for(var i = 0; i <= dt.length; i++){
        
    for(var key in dt1){
        //crear un objeto json con 15,000 items
        //Llenado de 'jsonaux'
        jsonaux.push(dt1[key]);  
        //console.log(jsonaux);

        contador =contador+1;
        filasrecorridas =filasrecorridas+1;

        //console.log(contador);
        //console.log(dt1[key]);

        if (contador==15000 || filasrecorridas==rowsavailables){
            console.log(jsonaux);
            //console.log(JSON.stringify(jsonaux));

            let data = new FormData();
            let jsonBlob = new Blob([JSON.stringify(jsonaux)], {type: "application/json"});

            data.append("datajson", jsonBlob);
            data.append("Flagcreartxt",Flagfiletxt);
            data.append("TxtName",TxtName);

            /*console.log(data.has('datajson'));
            console.log(data.has('Flagcreartxt'));
            console.log(data.has('TxtName'));*/
            
            fetch("model/importBud.php", {
            method: "POST",
            body: data
            })   
            .then(resp => {
                if(!resp.ok) {
                    const err = new Error("Response wasn't okay");
                    err.resp = resp;
                    throw err;
                }
                console.log(resp.text);
                alert('Ready Post...');
            }).catch(err => {
                console.error(err);
                alert('Error Post...');
                return;
    
            });

            Flagfiletxt=false;
            contador=0;
            jsonaux=[];
            jsonBlob = undefined; 
            delete jsonBlob; 
            data = undefined; 
            delete data;
        }
    } 
    
   return true;
        
}

0 Answers
Related