Dev Helpers
← Back

Return a typed fallback on rejection

Return a typed fallback on rejection using TypeScript.

Open this example in the interactive library

Source code

const load = async (id: number): Promise<{ id: number; ready: boolean }> => ({
  id,
  ready: true,
});
console.log(
  await Promise.reject<number>(new Error("offline")).catch((): number => 0),
);

Sample output

0