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

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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