JavaScript theorytheory 0/50 · 0%
Async · medium

29. async/await and error handling

Writing asynchronous code that reads like synchronous code.

`await` unwraps a promise; a rejection becomes a thrown error you catch with `try/catch`. An async function always returns a promise.

try {
  const r = await tx.wait();
} catch (e) {
  console.error(e.shortMessage ?? e.message);
} finally {
  setPending(false);
}

Never leave a floating promise: unhandled rejections crash Node and are silently lost in browsers.

Check your understanding

  1. 1. What does an async function return?

  2. 2. How is a rejection surfaced by `await`?

  3. 3. What is a floating promise?