Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
returning values from fs.readdir by using Promise in node js /javascript
function fileType() {
  return new Promise((resolve, reject) => {
    let Fields = fs.readdir(
      path.resolve(__dirname, "access"),
      (err, files) => {
        if (err) {
          reject(err);
          return;
        } else {
          files.map((file) => {
            return file.split(".").slice(0, -1).join(".");
          });
        }
      }
    );
    resolve(Fields);
  });
}

async function ambitious(data) {
  let fields = [];

  if (data.collections) {
   fields= await fileType();
  } else {
     if(data.types.access){
       fields.push(data.config.access);
     }
     else{
      fields= await fileType();
     }
  }

  return {
    data_name: data.name,
    data_access: data.config,
    data_encrypted: data.encrypt,
    data_fields: fields,
  };
}

Here I am trying to return field value from the if statement and store it in the data_fields

here problem is I am getting the values from the else statement but when I am trying to return value from If statement its showing undefined

As I want to read all the files which are present inside the access and remove the file extension and as an array I want to store it in data_fields

for eg if files are present inside access

let files=[ 'config.sql','admin.xml', 'user_manual.txt' ]

for that purpose I used split and slice to remove all the extension and return data in array format in data_fields

I don't know how to return value from fs.readdir and store it in data_fields

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Node already offers a Promise version of the fs API. You don't need to wrap the callback version in a Promise.

import { promises } from "fs";

async function fileType () {
    let files = await promises.readdir(path.resolve(__dirname, "access"));
    return files.map( file => file.split(".").slice(0, -1).join("."));
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try like this

function fileType() {
  return new Promise((resolve, reject) => {
    fs.readdir(
      path.resolve(__dirname, "access"),
      (err, files) => {
        if (err) {
          reject(err);
        } else {
          resolve(files.map((file) => { // resolve here
            return file.split(".").slice(0, -1).join(".");
          }))
        }
      }
    );
    // if you resolve you promise here, you resolve it before the readdir has been resolved itself.
  });
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda