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

174
Vistas
How do I convert this await call to a promise style .then call
async function getRedisKeyAsync(key){
    var value = await client.get(key);
    console.log('got value ' + value + ' for key ' + key + ' in redis');
    return value;
}

async function getRedisKeyPromise(key){
    client.get(key).then(
        (value) => {
            console.log('got value ' + value + ' for key ' + key + ' in redis');
            return value;
        }
    );
}

In both cases, value for key is fetched from redis correctly. But a caller of these functions get the value back only in the first case.

var value1 = await myredis.getRedisKeyAsync(req.params.key); // works

var value2 = await myredis.getRedisKeyPromise(req.params.key); // value2 is undefined

How do I rewrite getRedisKeyPromise so that it behaves the same as getRedisKeyAsync ?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Try by adding return to second function

async function getRedisKeyPromise(key) {
  return client.get(key).then(
    (value) => {
      console.log('got value ' + value + ' for key ' + key + ' in redis');
      return value;
    }
  );
}
about 4 years ago · Santiago Trujillo Denunciar

0

You can just simplify it:

function getRedisKeyPromise(key){
    return client.get(key)
}

It does the equivalent task like async or just .then()

about 4 years ago · Santiago Trujillo 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