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

186
Visualizações
Wait for the file to download completely before downloading the next one

I have an area for uploading files, with the ability to drag and drop multiple files, when uploading, I need to add the position of each file to the request(filePosition). The file position is the length of the array that is filled in then after the files are loaded. The question is how to make the next fetch call run only after the file object is added to the array

let materialFiles: [{
    lang: 'en',
    files: {
      restFiles: []
    }
  }, ],

  let files = e.target.files || e.dataTransfer.files;
let allowFiles = this.validateFiles(e, files, curObj);

Array.from(allowFiles).forEach(file => {
  attachThumbnail(curObj, file, e.target)
});

async attachThumbnail(curObj, file, eventTarget) {
    let index = this.formData.materialFiles.findIndex(item => item.lang === curObj.lang);
    let filePosition = curObj.files.restFiles.length;

    this.uploadFile(curObj, file, 'rest', filePosition)
      .then(response => response.json())
      .then(data => {
        this.formData.materialFiles[index].files.restFiles.push({
          name: data.fileName,
          id: data.id
        })
      })
      .catch(error => {
        console.log(error)
      });
  },

  uploadFile(curObj, file, type, position) {
    let formData = new FormData(),
      data = {
        document: file,
        type: type,
        lang: curObj.lang,
        position: type === 'rest' ? position : 0
      }

    for (let key in data) {
      formData.append(key, data[key]);
    }
    fetch('/api/material_media', {
      method: 'POST',
      body: formData,
    })
  },

In my case, fetch is performed first and only then push to then occurs. It is necessary that first go to then and add an object to the array (so that its length changes) and then fetch is called for the next file.

about 4 years ago · Juan Pablo Isaza
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