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

167
Visualizações
Is there a better way of writing a function with two async?

I have this function:

private async get(iD: string): Promise<Function> {
        return async () => {
            const tallConfig = await longCrudService.getHalf(iD);
            const stream: Stream = tallConfig.stream;

            const response = this.createUrl(tallConfig, stream);
            return response;
        };
    }

Is there a way I could re-write this but without using double async, like is there a way I could re-write this by using only the async of the function, and removing the inner async?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The only decent use of async is when you need to await directly inside. Since you aren't doing so for the outer function, you can remove it - only the inner function is making use of an await.

private get(iD: string): Promise <Function> => async () => {
    const tallConfig = await longCrudService.getHalf(iD);
    const stream: Stream = tallConfig.stream;
    const response = this.createUrl(tallConfig, stream);
    return response;
}

You could also remove the asyncs entirely if you wanted, in exchange for .then.

private get(iD: string): Promise <Function> => () => longCrudService.getHalf(iD)
    .then(tallConfig => this.createUrl(tallConfig, tallConfig.stream));
about 4 years ago · Juan Pablo Isaza Relatório

0

There's no need for an inner function you can rewrite as such:

private async get(iD: string): Promise<Function> {
    const tallConfig = await longCrudService.getHalf(iD);
    const stream: Stream = tallConfig.stream;

    return this.createUrl(tallConfig, stream);
}

Make sure that the caller does not need another function call as this would return a promise as specified on your function signature.

about 4 years ago · Juan Pablo Isaza 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