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
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 Respuestas
Responde la pregunta

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 Denunciar

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