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

123
Views
¿Cómo agregar el manejo de errores a una llamada de función asíncrona?

Actualmente el código mecanografiado tiene esto:

 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 }; }, []);

Ahora sabemos que el método selectAsync() devuelve una promesa:

 selectAsync(): Promise<SelectResponse>

Y SelectResponse podría contener un mensaje de error:

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

Entonces, en este caso, cómo agregar código de manejo de errores para await cleanSteps[0].selectAsync(); ? ¿Debería eliminarse aquí la palabra clave await ?

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

0

Entonces, básicamente, el método selectAsync maneja los errores (aquellos que se lanzan como Error) y devuelve el objeto de respuesta según el éxito o el fracaso.

Puede obtener esa respuesta por

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

y comprobar

 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 }

Realmente depende de lo que quieras hacer en cada escenario.

Si desea saber cómo manejar los errores de la función asíncrona en general, debe envolver el contenido que podría arrojar un error con el bloque try catch.

 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 } } );

Cadena de promesas en asíncrono/espera

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!