Question T15382
Visible to All Users

Async Functions in Calculated Values

created a year ago (modified a year ago)

Hello,
I am trying to use an async function in the calculated values of SurveyJS. The async function itself works as I have verified that it properly outputs the correct values to console, however, that value is not reflected in SurveyJS when using the calculated value. I am assuming this is due to the survey being displayed/rendered before function finishes. While looking through other tickets I found a ticket quite similar to my issue, though no solution came out of it (https://surveyjs.answerdesk.io/ticket/details/t14415). I have attached a few images that should help visualize the issue. When running the survey, the {lastUpdated} is null, though, console says it is not. Not sure whether I am missing a SurveyJS setting when using async functions but I have added that the "lastUpdated" function is async when registering it to the FunctionFactory.

JavaScript
const lastUpdated = useCallback(async() => { try { const res = await axios.get("{URL}", { params: { userID: tokenPayload.sub } }) console.log(JSON.parse(res.data.body).Item.lastUpdated); return JSON.parse(res.data.body).Item.lastUpdated; } catch (error) { console.log(error); return "Never"; } }) FunctionFactory.Instance.register("lastUpdated", lastUpdated, true);

Thank you.

Answers approved by surveyjs Support

created a year ago

Hello Sagar,
You don't use async function correctly. Please follow our documentation and review the following example.

Thank you,
Andrew
SurveyJS Team