Skip to main content

I wrote a plugin that is working fine. But when I try to refactor some code into functions the same functionality stops to function. It seems that any code I place into nested async functions never return a value. I also tried the “then” syntax.


Is this a known issue and how do I resolve this?


In the example below async function updateElements() is an async function nested in a function that is already async. Any await figma.... does not return (so it is not just this function). This sin’t really covered in the documentation I read, for example Asynchronous Tasks | Plugin API


async function updateElements() {
console.log('This line appears in console, indicating the function is executed');
const colorVariable = await figma.variables.getVariableByIdAsync('VariableID:2:492');
console.log('Never get\'s here');
}

figma.ui.onmessage = async (msg: { type: string, background: string, color: string }) => {
if (msg.type === 'update-elements') {
const colorVariable = await figma.variables.getVariableByIdAsync('VariableID:2:492');//this works
console.log(colorVariable); // Contents are displayed in console
await updateElements();
}
}

I figured out how to solve the immediate issue:

I initially followed the quickstart guide here: Plugin Quickstart Guide | Plugin API which creates a tsconfig.json:


{
"compilerOptions": {
"target": "es6",
"lib": ["es6"],
"strict": true,
"typeRoots": [
"./node_modules/@types",
"./node_modules/@figma"
]
}
}

When I change strict so it reads strict: false, then the code works as expected


Reply