2

So i have been searching, is using a finally really necessary, and i guess i haven't found the answer i was looking for my type of use case. Here is the code example:

async fetchFromBackend() {
  this.isLoading = true;
  try {
    const res = await fetchData();
    return res;
  } catch (e) {
    console.log(e);
  }
  this.isLoading = false;
}

from the code above, the reassignment of this.isLoading won't be executed until it has done with the await without needing to put it inside a finally regardless it will throw an error or not. Can someone elaborate if this would be the best practice or if the reassignment outside of try...catch suffice?

dunhilblack
  • 195
  • 1
  • 4
  • 13
  • "finally" runs regardless. So thats how you would use it to preserve scope. – GetSet Feb 05 '21 at 16:22
  • In general, `catch` would not necessarily catch *all* exceptions (ignoring them by logging them might not be the best approach here either, the function then returns `undefined`) and might itself throw an exception. For these cases, a `finally` block makes more sense - only in your example it hardly makes a difference. – Bergi Feb 05 '21 at 17:28

0 Answers0