Refactor query execution logic to return script results and handle errors properly

This commit is contained in:
lborv
2025-09-17 17:28:06 +03:00
parent 750e7125ad
commit d90c85d66f
2 changed files with 38 additions and 11 deletions

View File

@ -29,7 +29,41 @@ export class Vm {
}
async runScript(script: string) {
const compiledScript = await this.context.isolate.compileScript(script);
return compiledScript.run(this.context);
let resolvePromise: (value: any) => void;
let rejectPromise: (reason?: any) => void;
const resultPromise = new Promise<any>((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});
this.setFunction("result", (...args) => {
console.log("Script result:", args);
resolvePromise(args);
});
this.setFunction("error", (error: any) => {
console.error("Script error:", error);
rejectPromise(error);
});
const scriptWithResult = `
(async () => {
${script}
try {
const result = await main()
result(result)
} catch (e) {
error(e)
}
})()
`;
const compiledScript = await this.context.isolate.compileScript(
scriptWithResult
);
compiledScript.run(this.context);
return await resultPromise;
}
}