Figma Support Forum

loadFontAsync Multiple Fonts (MEMORY ERROR)

Hi Figma devs :smiley:

I’m trying to load all fonts before updating layer character.

const fontArray = [
  { family: 'Roboto', style: 'Regular' },
  { family: 'Roboto', style: 'Bold' },
  { family: 'Font Awesome 5 Pro', style: 'Solid' },
  { family: 'Font Awesome 5 Pro', style: 'Light' },
];

fontArray.map(async (font) => {
  await figma.loadFontAsync(font);
});

Unfortunately, I get a memory error. Can somebody help, please?
Thanks in advance

You can’t use async in a map so this works synchronously and likely the fonts aren’t loaded before you try to change the characters. Also all these four calls would happen at the same time, a memory error may be caused by trying to load multiple fonts at the same time.

Why not load all fonts without manually specifying them?

selection.forEach(async (font) => {
await figma.loadFontAsync(font.fontName as FontName);
});

I got it working already :grinning: Thank you for the quick responses.

I’m doing it like this:

await figma.loadFontAsync(node.fontName as FontName);
node.characters = value;
2 Likes

Smashing! This took me an age to figure out myself haha.