async function을 바로 return하면 error를 catch 하지 못한다.

let count = 0
function asyncFunc() {
  return new Promise((resolve, reject) => {
    if(count % 10 === 0) {
      count++
      reject(new Error('error!!'))
      return
    }
    resolve(++count)
  })
}

async function bad() {
  try {
    // await 하지 않고 바로 리턴하면 에러를 캣치하지 못한다.
    return asyncFunc()
  } catch(e) {
    console.log(e.message)
    return asyncFunc()
  }
}

async function right() {
  try {
    const res = await asyncFunc()
    return res
  } catch(e) {
    console.log(e.message)
    return asyncFunc()
  }
}

test('test async reject', async t => {
  for(let i = 0; i < 100; i++) {
    const res = await right()
    console.log(res)
  }
  t.pass()
})
error!!
2
3
4
5
6
7
8
9
10
error!!
12
.
.
.
110
error!!
112