Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
JavaScript - wait for a function to finish processing before going to the next iteration of the loop

I would like to make one function wait for another to finish completely. We have this function that processes a list of files and loads each one of them into the loadFileIntoMemory function.

I would like this function to wait for the loadFileIntoMemory function to finish manipulating and working on said file before going to the next iteration of the loop and calling it with the next file.

function processFiles(files) {
  for (let i = 0; i < files.length; i++) {
    console.log("Loading File into memory");
    loadFileIntoMemory(files[i]);
  }
}

So it would load file number 1, wait for everything to be done and over, then load file 2. I don't want it to spam files at the loadFileIntoMemory function without it finishing the previous file.

function loadFileIntoMemory(file) {
  
  //create a dictionary using the file name
  originalValues[file.name] = {};
  // create a variable with the file name to pass to storeOriginalValues and replaceOccurence
  var filename = file.name;
  // Start reading the file
  var reader = new FileReader();
  reader.onload = function (file) {
    var arrayBuffer = reader.result;
    // Here we have the file data as an ArrayBuffer.  dicomParser requires as input a
    // Uint8Array so we create that here
    var byteArray = new Uint8Array(arrayBuffer);
    dataSet = dicomParser.parseDicom(byteArray);
    // Store the original values into the dict
    storeOriginalValues(filename);

    // iterate through each metadata field and replace the original value with the de-identified value.
    console.log("De-identifying values")

    for (const [key, value] of Object.entries(elementsToRemove)) {
      var element = dataSet.elements[value];
      if (typeof element !== "undefined") {
        for (var i = 0; i < element.length; i++) {
          dataSet.byteArray[element.dataOffset + i] = 32;
        }
      }
    }

    // Check if the Patient ID is still Present after the de-indentification process is completed
    replaceOccurences(filename, originalValues[filename].PatientID);
    // replaceOccurences(filename, originalValues[filename].PatientName);
    // replaceOccurences(filename, originalValues[filename].etc);

    // download the de-identified DICOM P10 bytestream
    console.log('Downloading File');
    var blob = new Blob([dataSet.byteArray], { type: "application/dicom" });
    var url = window.URL.createObjectURL(blob);
    window.open(url);
    window.URL.revokeObjectURL(url);
    
  };
  reader.readAsArrayBuffer(file);
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You will need to convert your loadFileIntoMemory into a Promise.

function loadFileIntoMemory(file) {
    return new Promise((resolve,reject) => { 
        ......
        reader.onload = function (file) {
           ......
           return resolve();  //when all is done, resolve the promise.
        }

    })
}

Then you can use the function in a async/await loop.

const processFiles = async (files) => {
  for (let i = 0; i < files.length; i++) {
    console.log("Loading File into memory");
    await loadFileIntoMemory(files[i]);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda