Concepts

Await inside a catch boundary

Try the free JavaScript compiler

Awaiting the operation inside try allows its rejection to be handled by that catch. Returning the promise directly does not catch a later rejection there.

Formal Syntax & Structure

javascript syntaxjavascript
try { return await operation(); } catch (error) { ... }

Working Example

Example Snippetjavascript
(async () => {async function run(work){try{return await work();}catch{return "failed";}}console.log(await run(()=>Promise.reject(new Error("x"))),await run(()=>Promise.resolve("ok")));})()
JavaScript Reference