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

155
Views
How to return promise with modifications from async function?

I have an async function and I need to do the following in this function:

  • If ajaxRequest succeeds then I must process value and return it in Promise,
  • Otherwise return Promise with null.

This is what I did:

 public async foo(): Promise<B> {
    const promiseA: Promise<A> = ajaxRequest(...);
    promiseA.then((a) => {
      return new Promise<B>((resolve) => {
         const b: B = process(a); 
         resolve(b);
      })
    });
    promise.catch(e => {
      console.error(e);
      return new Promise<B>((resolve) => {
         resolve(null);
      })
    })
  }

And I get A function whose declared type is neither 'void' nor 'any' must return a value. compilation error. Could anyone say how to do it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your function seems to simplify to

class Something {
  public async foo(): Promise<B | null> {
    try {
      const a = await ajaxRequest(...);
      return process(a);
    } catch (e) {
      console.error(e);
      return null;
    }
  }
}

assuming process returns B.

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!