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

120
Views
How to add error handling to an async function call?

Currently the typescript code has this:

  React.useEffect(() => {
    if (prepAttrsRef.current) {
      prepAttrsRef.current.addEventListener('documentstatechanged', 
      async (evt: CustomEvent<FlowDocumentState>) => {
        if (evt.detail.draftState === 'all-changes-published') {
          dispatch(updateIsFlowpublished(true));
          let cleanSteps: readonly CleanStep[] = [];
          if (prepAttrsRef.current) {
            cleanSteps = await prepAttrsRef.current.getAllStepsOfTypeAsync('clean');
            if (cleanSteps && cleanSteps.length > 0 && cleanSteps[0] !== undefined) {
              await cleanSteps[0].selectAsync();
            }
          }
        }
      });
    }
    return () => {
      //TBC: cleanup
    };
  }, []);

Now we know that selectAsync() method returns a promise:

selectAsync(): Promise<SelectResponse>

And the SelectResponse could contain error message:

SelectResponse: { success: true } | { errorMessage: string; errorType: SelectError; success: false }

So in this case, how to add error handling code to await cleanSteps[0].selectAsync(); ? Should the await keyword to be removed here?

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

0

So basically selectAsync method handles errors (those that are throw as and Error) and returns response object depending on success or failure.

You can get that response by

const response = await cleanSteps[0].selectAsync();

And check

const response = await cleanSteps[0].selectAsync();
if (response.success) {
   // Do some success handling
} else {
  // Do some error handling, you have { errorMessage: string; errorType: SelectError; success: false } object here
}

It really depends on what you want to do in each scenario.

If you want to know how to handle async function errors in general then you need to wrap content that might throw an error with try catch block

prepAttrsRef.current.addEventListener(
  'documentstatechanged',
  async (evt: CustomEvent<FlowDocumentState>) => {
    try {
      if (evt.detail.draftState === 'all-changes-published') {
        dispatch(updateIsFlowpublished(true));
        let cleanSteps: readonly CleanStep[] = [];
        if (prepAttrsRef.current) {
          cleanSteps = await prepAttrsRef.current.getAllStepsOfTypeAsync(
            'clean'
          );
          if (
            cleanSteps &&
            cleanSteps.length > 0 &&
            cleanSteps[0] !== undefined
          ) {
            await cleanSteps[0].selectAsync();
          }
        }
      }
    } catch (error) {
      // Do some error handling
    }
  }
);

Promise chain in async/await

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!