Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!