1

I have a doubt I wrote a program and I am confused what to use to handle if any error occur in my loop and which is best practice to write to handle error handling

should I use then catch or try catch in my for of loop as the output

for (value of Data){

test = await getValue(value)
       .then((obj)=>{
         // some code})
       .catch((err)=>{
         console.log(err);});
}
for (value of Data){
 try{
 test= await getValue(value);
 }
 catch (e){
  console.log(e);
}

Ps: down vote welcome but need proper explanation which is best practise to write

Aakash
  • 139
  • 1
  • 3
  • 22
  • 1
    mixing `async`/`await` with `.then` `.catch` in a single function is (almost) always bad code - pick one and stick with it – Bravo Jul 26 '21 at 05:11
  • 2
    This is a personal preference. Usually, you would use `try/catch` with `await` and `.catch()` when not using `await`, but there are exceptions. Also, you would generally NOT use `.then()` when using `await`. The whole point of `await` is to avoid `.then()` and the nested code it causes. – jfriend00 Jul 26 '21 at 05:11
  • so should i go with try catch ? @Bravo – Aakash Jul 26 '21 at 05:12
  • see previous comment by jfriend00 - it's not up to me, it's not up to him, it's purely up to you - of the two pieces of code, the second is arguably "better" as it's not mixing async/await with .then/.catch - so in that respect - make your mind up – Bravo Jul 26 '21 at 05:12
  • got it @jfriend00 :) – Aakash Jul 26 '21 at 05:13
  • 2
    Inside, your `for` loop, `await` vs. `.then()` gives completely different results. One offers parallel running of asynchronous operations, the other offers sequential running of asynchronous operations. So, you use the one that gives you the behavior you want. Then, pick the error handling method that matches (`try/catch` with `await` and `.catch()` with `.then()`). – jfriend00 Jul 26 '21 at 05:14
  • yeah, hadn't even considered the `for` loop – Bravo Jul 26 '21 at 05:14

1 Answers1

2

.catch() vs. try/catch is somewhat a personal preference and it also depends upon how you want the code to run. Usually, you would use try/catch with await and .catch() when not using await, but there are exceptions. Also, you would generally NOT use .then() when using await. The whole point of await is to avoid .then() and the nested code it causes.

Inside, your for loop, await without a .then() vs. .then() with await gives completely different results. One offers parallel running of asynchronous operations, the other offers sequential running of asynchronous operations as the for loop pauses until the await fulfills.

So, you use the one that gives you the behavior you want. Then, pick the error handling method that matches (try/catch with await and .catch() with .then() - usually).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • "*Inside your for loop, `await` vs. `.then()` gives completely different results.*" - OP had `await` in both snippets – Bergi Jul 26 '21 at 08:06
  • @Bergi - Yes, and I've clarified my answer in that regard. But, the spirit of the question was about `.catch()` vs. `try/catch`. – jfriend00 Jul 26 '21 at 22:35