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

189
Visualizações
Cómo determinar si se completa un bucle de autorreferencia de Javascript

Tengo una situación en la que necesito leer todos los archivos en un directorio y todos sus subdirectorios. Escribí una función básica:

Aunque esto está en React-Native, no estoy seguro de que importe (o tal vez haya una solución de reacción nativa).

 let fileList = []; const readDir = async (dir) => { // This line simply reads the contents of a directory, creating an array of strings // of all the files and directories inside 'dir'. const items = await FileSystem.readDirectoryAsync(dir); items.forEach(async (item) => { const f = await FileSystem.getInfoAsync(item); // Gets basic information about the item if (f.isDirectory === true) { readDir(f.uri); // f.uri is the location of the new directory to read } else { // runs if the item is a file console.log("f.uri: ", f.uri); fileList.push(f.uri); // A global variable } }) }; const parentDirectory = "parent_folder"; readDir(parentDirectory); // Do more stuff here once all files have been read and added to 'fileList'

Esto parece funcionar parcialmente ya que todos los archivos en todos los subdirectorios se consolan desde dentro del segmento else {...}.

Sin embargo, ¿cómo puedo saber que el ciclo está completo para poder continuar con el script y usar 'fileList'?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

No use forEach con async , no funcionan juntos y no puede esperar el bucle. Use un estándar for .. of bucle, que funciona bien con async/await

 const readDir = async (dir) => { const items = await FileSystem.readDirectoryAsync(dir); for (let item of items) { const f = await FileSystem.getInfoAsync(item); if (f.isDirectory === true) { await readDir(f.uri); } else { console.log("f.uri: ", f.uri); fileList.push(f.uri); } } }

Y como readDir es async , por supuesto, también debe await o usarlo para then una vez que haya terminado.

 async function foo() { const parentDirectory = "parent_folder"; await readDir(parentDirectory); //do some other stuff once readdir is finished. }

o

 const parentDirectory = "parent_folder"; readDir(parentDirectory).then(_ => { //do some other stuff once readdir is finished. }
about 4 years ago · Santiago Gelvez 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