Skip to content

lingonsaft/return-fn_vs_return-await-fn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

return-fn_vs_return-await-fn

Do you know the difference between "return await bar()" and "return bar()" inside an async function?

Using the await keyword to reslove an async function.

async function foo () {
  return await asyncFn()
}

Resloves the async function without await keyword.

async function foo () {
  return asyncFn()
}

With the await keyword we can catch errors inside foo

async function foo () {
  try {
    return await asyncFn()
  } catch (error) {
    // here we catch errors from asyncFn.
  }
}

Without the await keyword the error is passed outside .catch

async function foo () {
  try {
    return asyncFn()
  } catch (error) {
    // errors from asyncFn do not land here.
  }
}

foo()
  .catch((error) => {
    // error from foo will land here.
  })

Releases

No releases published

Packages

No packages published