Akiq2016/blog

difference between return await promise and return promise

Opened this issue · 0 comments

difference between

function test1 () {
  try {
    return waitASecond();
  } catch (e) {
    return 'ahhhhh';
  }
}
function test2 () {
  try {
    return await waitASecond();
  } catch (e) {
    return 'ohhhhh';
  }
}

Disallows unnecessary return await (no-return-await)

Inside an async function, return await is seldom useful. Since the return value of an async function is always wrapped in Promise.resolve, return await doesn’t actually do anything except add extra time before the overarching Promise resolves or rejects. The only valid exception is if return await is used in a try/catch statement to catch errors from another Promise-based function.

Refs

https://jakearchibald.com/2017/await-vs-return-vs-return-await/
https://github.com/eslint/eslint/blob/master/docs/rules/no-return-await.md