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

133
Vistas
Return value from callback

I have value inside callback function and couldn't return successfully.

async pos(convertedCodeDBSearch) {
    var dd;
    const x =  await idealPostcodes.lookupAddress(convertedCodeDBSearch, function (error, searchResults)  {
      return searchResults.result.hits[1];
    });
    console.log('x', x);
  }

I need to return searchResults.result.hits[1]; from function. Here is the code, Thank you.

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

0

I imagine you are using this npm package or a fork of it. If this is the case, you should revise your logic, because the call to lookupAddress does not return a value by itself, meaning that the x in your code will always be undefined.

Since you are already working with async functions, my suggestion is using the Node helper util.promisify to extract the result from the callback function into a promise. This promise can be awaited to get the success value of the original callback (here searchResults).

const { promisify } = require('util');

async pos(convertedCodeDBSearch) {
    var dd;
    const searchResults = await promisify(idealPostcodes.lookupAddress.bind(idealPostcodes))(convertedCodeDBSearch);
    return searchResults.result.hits[1];
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Not sure if it'll solve you're problem, but why don't you get rid of the callback entirely and utilize the async/await syntax to it's fullest?

async pos(convertedCodeDBSearch) {
    var dd;
    try {
        const x =  await idealPostcodes.lookupAddress(convertedCodeDBSearch);
        const results = x.result.hits[1];
        console.log(`The results are: ${results}`);
        return results;
    } catch (e) {
        console.error("Something went wrong")
    }
}
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