document.fonts.load Awaits The Icon Font

document.fonts.load requests the faces for glyphs about to render and resolves with FontFace[], so icons draw only after the family is really available.

TypescriptResilience

Code

typescriptsnippet
17 lines
async function drawStatusBadge(): Promise<void> {
  // Ask for the exact faces the icon glyphs need; resolves with FontFace[]
  // once those glyphs are downloadable and usable.
  const loaded = await document.fonts.load('700 16px "OpsIcons"', '✓ ✗ ↻');

  // An empty result with a failing check means the family is not available
  // at all: fall back to text instead of drawing tofu boxes.
  if (loaded.length === 0 && !document.fonts.check('700 16px "OpsIcons"')) {
    console.log('icon font unavailable — falling back to text labels');
    return;
  }

  const families = loaded.map((face) => face.family).join(', ');
  console.log('icon fonts ready:', families || 'already loaded');
}

drawStatusBadge();

Walking through the code — document.fonts.load Awaits The Icon Font

The snippet is organized around the function drawStatusBadge.

The async parts await their results, which keeps ordering predictable.

What to watch for

  • Your existing toolchain is sufficient; nothing beyond it is needed.
  • This is browser-side code — document will not resolve in Node.

Using it

Entry signature: async function drawStatusBadge()

The run prints icon fonts ready: — each prefix names the step that produced it.

Key statements

const loaded = await document.fonts.load('700 16px "OpsIcons"', '✓ ✗ ↻');
const families = loaded.map((face) => face.family).join(', ');

Interactive Tools for This Topic

FAQ

Does document.fonts.load Awaits The Icon Font run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.