5

Throwing error to upper level in an async function

This

async create(body: NewDevice, firstTry = true): Promise<RepresentationalDevice> {
  try {
    return await this.dataAccess.getAccessToken()
  } catch (error) {
    throw error
  }
}

VS this

async create(body: NewDevice, firstTry = true): Promise<RepresentationalDevice> {
  return await this.dataAccess.getAccessToken()
}

I mean at the end on the upper level I must catch the error anyway and there is no modifications at all on the catch

Are these two approaches identical? Can I use the second approach without error handling issues?

Iván E. Sánchez
  • 1,183
  • 15
  • 28
  • FYI, `async` functions are part of ES2017, not ES7 (ES2016). – Felix Kling Aug 24 '17 at 18:10
  • What is your question? Are you asking whether it is OK to use the second example? – Felix Kling Aug 24 '17 at 18:11
  • 2
    Yes, both of these are the same. They’re also the same as `return this.dataAccess.getAccessToken()` (the only time `return await` isn’t equivalent to `return` is inside a `try`). – Ry- Aug 24 '17 at 18:12
  • yep that was my question. So it's exactly the same right? – Iván E. Sánchez Aug 24 '17 at 18:14
  • Please explain this. what is that? "the only time return await isn’t equivalent to return is inside a try" @Ryan – Iván E. Sánchez Aug 24 '17 at 18:15
  • I’m not really sure how else to put it. If you return a promise in an async function, it gets chained to the previous `await`. If you’re in a `try` and the promise being `await`ed is rejected, the `try` can catch that, but otherwise they’re the same. – Ry- Aug 24 '17 at 18:27
  • Oh yeah but that's only if I don't reject the promise on the catch right? – Iván E. Sánchez Aug 24 '17 at 19:05

1 Answers1

1

This has nothing to do with async functions. Catching an error just to rethrow it is the same as not catching it in the first place. I.e.

try {
  foo();
} catch(e) {
  throw e;
}

and

foo();

are basically equivalent, except that the stack trace might be different (since in the first case the error is thrown at a different location).

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Well, then I can't understand why do you say that "this has nothing to do with `async` functions" `foo();` in this case is a promise so I need to add the await keyword to return the promise solved – Iván E. Sánchez Aug 24 '17 at 20:43
  • I'm saying the behavior of try catch has nothing to do with async functions. Or am I not understanding what your question is about? – Felix Kling Aug 24 '17 at 20:45
  • Yes, I'm sorry, I was not clear enough about what was the question. Basically the question was. Are these two approaches identical? Can I use the second approach without error handling issues? – Iván E. Sánchez Aug 24 '17 at 20:52
  • 1
    That's what I meant: these cases are the same whether you are using an async function or not. – Felix Kling Aug 24 '17 at 20:54
  • 1
    Oh I can see, because anyway (sync or async) the try-catch throwing the exception on the catch is identically to not having even that block. Because in any case the exception will be thrown to the upper level. – Iván E. Sánchez Aug 24 '17 at 21:07