Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unhandled promise rejection propagation #85

Open
pmomot opened this issue Jul 13, 2022 · 2 comments
Open

Fix unhandled promise rejection propagation #85

pmomot opened this issue Jul 13, 2022 · 2 comments

Comments

@pmomot
Copy link

pmomot commented Jul 13, 2022

In executeAsyncOperation the promise rejection scenario is not properly handled. It captures the error, saves it in state, but then it propagates further and causes exceptions in the codebase. I suggest to change the implementation to this

return promise
      .then((result) => {
        if (shouldHandlePromise(promise)) {
          AsyncState.setResult(result);
        }
        normalizedOptions.onSuccess(result, {
          isCurrent: () => CurrentPromise.is(promise),
        });
      })
      .catch((error) => {
        if (shouldHandlePromise(promise)) {
          AsyncState.setError(error);
        }
        normalizedOptions.onError(error, {
          isCurrent: () => CurrentPromise.is(promise),
        });
      });

instead of

promise.then(() => {}, () => {});
@damienromito
Copy link

damienromito commented Sep 23, 2022

I have the same probleme. I have to try cath my requests even if I use onError to handle error

@pig800509
Copy link

pig800509 commented Jun 6, 2023

const resultRef = useRef();
const { result } = useAsync(async( $params, $condition ){
   if($condition){
       return await yourPromiseFunction($params).then((response)=>{
       //handle your response
       if( response is error ){
           const error: Error = {
          name: "Error Occurred",
          message: "Error",
          stack: "no stack"
          console.error(error.name, res.ResultMessage);
          throw error;
        }
       }
     })
   }
},
[params, condition],
{
    setLoading: (state) =>  ({
      ...state
      result: resultRef.current ?? state.result,
      loading: true
  }),
    setResult: (result, asyncState) => ({
    ...asyncState,
    result:
      result === undefined ? resultRef .current : result,
    loading: false
  }),
    onSuccess: ( result , { isCurrent })=>{
      if (isCurrent()) {
        resultRef.current = result ?? resultRef.current;
        //do the rest
      }
    },
    onError:()=>{}
}
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants