From 918dffb4bc7a18da41862b90288bda8fe59246ae Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Mon, 17 Apr 2023 14:20:36 +0200 Subject: [PATCH] :bug: Await support in set variable and script code Closes #467 --- .../js/src/features/blocks/logic/script/executeScript.ts | 8 +++++++- .../blocks/logic/setVariable/executeSetVariable.ts | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/embeds/js/src/features/blocks/logic/script/executeScript.ts b/packages/embeds/js/src/features/blocks/logic/script/executeScript.ts index dd2688ff9..289b6d59a 100644 --- a/packages/embeds/js/src/features/blocks/logic/script/executeScript.ts +++ b/packages/embeds/js/src/features/blocks/logic/script/executeScript.ts @@ -1,7 +1,13 @@ import type { ScriptToExecute } from '@typebot.io/schemas' +// eslint-disable-next-line @typescript-eslint/no-empty-function +const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor + export const executeScript = async ({ content, args }: ScriptToExecute) => { - const func = Function(...args.map((arg) => arg.id), parseContent(content)) + const func = AsyncFunction( + ...args.map((arg) => arg.id), + parseContent(content) + ) try { await func(...args.map((arg) => arg.value)) } catch (err) { diff --git a/packages/embeds/js/src/features/blocks/logic/setVariable/executeSetVariable.ts b/packages/embeds/js/src/features/blocks/logic/setVariable/executeSetVariable.ts index 151e5abe6..b3e656efd 100644 --- a/packages/embeds/js/src/features/blocks/logic/setVariable/executeSetVariable.ts +++ b/packages/embeds/js/src/features/blocks/logic/setVariable/executeSetVariable.ts @@ -1,12 +1,15 @@ import { isNotDefined } from '@typebot.io/lib' import type { ScriptToExecute } from '@typebot.io/schemas' +// eslint-disable-next-line @typescript-eslint/no-empty-function +const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor + export const executeSetVariable = async ({ content, args, }: ScriptToExecute): Promise<{ replyToSend: string | undefined }> => { try { - const func = Function( + const func = AsyncFunction( ...args.map((arg) => arg.id), content.includes('return ') ? content : `return ${content}` ) @@ -15,6 +18,7 @@ export const executeSetVariable = async ({ replyToSend: safeStringify(replyToSend), } } catch (err) { + console.error(err) return { replyToSend: safeStringify(content), }