Why Catch is not working?

The API is sending me a 422, error but catch doesn’t trigger. Anyone knows what its happening?

  const handleDialogModalConfirm = async () => {
    try {
      selected.forEach(async jobApp => {
        // This is a simple POST <---
        await assessmentsApi.createResource({ job_application_id: jobApp.applicant.id })
      })
      showModal(t('assessment.modal.success.title'), t('assessment.modal.success.content'))
    } catch (err) {
      showModal(t('assessment.modal.error.title'), err || '')
    }
    setOpenChoiseDialog(false)
  }

What makes you think that it should be throwing?

What is happening inside assessmentsApi.createResource() is it a fetch call?

The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500.

What makes you think that it should be throwing?. Because it’s an error.
Its a POST.

I still do not know what is going on inside the method that makes you think it should be throwing. But as I posted fetch() won’t reject on HTTP error status, it will only reject on network failure.

const getInvalidEndpoint = async () => {
  try {
    const res = await fetch("https://jsonplaceholder.typicode.com/to");
    console.log(res.ok); // false
    console.log(res.status); // 404
  } catch (err) {
    console.error("error that will not be logged", err);
  }
};

getInvalidEndpoint();

Also, your use of forEach.

Note: forEach expects a synchronous function.

forEach does not wait for promises. Make sure you are aware of the implications while using promises (or async functions) as forEach callback.

const ratings = [5, 4, 5];
let sum = 0;

const sumFunction = async (a, b) => a + b;

ratings.forEach(async (rating) => {
  sum = await sumFunction(sum, rating);
});

console.log(sum);
// Naively expected output: 14
// Actual output: 0

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.