Refactor query execution logic to return script results and handle errors properly
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user