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

186
Vistas
Why is my Async Await not working in NodeJS?

Trying to perform a basic async/await in NodeJS but it is still going out of order.

Code I am trying to run:

var fs = require('fs');
var stringData;

async function find(context) {
  try {
    let text = await readFile('./sql/test.sql', context);
  } catch (e) {
    console.log('Try/Catch Error: ' + e)
  }

  console.log('display')
  console.log('display ' + JSON.stringify(text))
  return
}

async function readFile(file, context) {
  new Promise((resolve) => {
    fs.readFile(file, (error, data) => {
        if (error) {
          throw error;
        }
        stringData = data.toString()
      }),
      (err) => {
        console.log('ERROR: ' + JSON.stringify(err));
      }, () => {
        resolve(1);
      }
  })
};

module.exports.find = find;

When I run the above I get an undefined results for the console.log for text.

I am expecting text to use the await to populate before moving down to the console logs.

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

0

Looks like you are not resolving the promise with the result you get from data.toString(); you need to add resolve(stringData)

PS. In the error handler you are using resolve, you should probably be using reject there

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