From 65958d6cd613a831f8350d509189d1230a8c4a31 Mon Sep 17 00:00:00 2001 From: Jackie Yang Date: Wed, 22 Nov 2023 00:45:39 -0800 Subject: [PATCH] Improve promise handling (one by one instead of all together) --- lib/dsl/dsl-interpreter.ts | 20 ++++++++++++++------ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/dsl/dsl-interpreter.ts b/lib/dsl/dsl-interpreter.ts index 84d6788..0745955 100644 --- a/lib/dsl/dsl-interpreter.ts +++ b/lib/dsl/dsl-interpreter.ts @@ -6,6 +6,14 @@ import { ParamDescriptor, } from "../dsl-descriptor"; +async function promiseOneByOne(promiseList: Promise[]) { + let result = []; + for (const p of promiseList) { + result.push(await p); + } + return result; +} + function parseType(value_type: string): { is_array: boolean; original_type: string; @@ -356,7 +364,7 @@ export class DslInterpreter { case "array": return { type: "array", - elements: await Promise.all( + elements: await promiseOneByOne( ast.value.map(async (e) => await this.describe(e))), }; case "string": @@ -382,7 +390,7 @@ export class DslInterpreter { } public async describeSteps(list: any[]) { - return await Promise.all(list.map((e) => this.describe(e.result))); + return await promiseOneByOne(list.map((e) => this.describe(e.result))); } /** @@ -472,7 +480,7 @@ export class DslInterpreter { ); if (classDescriptor === undefined) { if (parent.type == "array") { - const arrayValue = await Promise.all(parent.value.map( + const arrayValue = await promiseOneByOne(parent.value.map( (v: any) => this.resolveAccess({ ...ast, parent: v @@ -577,7 +585,7 @@ export class DslInterpreter { */ private async resolveArray(ast: any, env: any) { console.assert(env == null); - const values = await Promise.all(ast.value.map((v) => this.resolve(v, null))); + const values = await promiseOneByOne(ast.value.map((v) => this.resolve(v, null))); // make sure all the elements are objects console.assert(values.every((v) => v.type === "object")); // make sure all the elements have the same type @@ -597,7 +605,7 @@ export class DslInterpreter { private async resolveFunctionCall(ast: any, env: any) { const parameters = ast.parameters !== null - ? new Map(await Promise.all( + ? new Map(await promiseOneByOne( ast.parameters.map(async (p) => [ p.parameter, await this.resolve(p.value, null), @@ -650,7 +658,7 @@ export class DslInterpreter { (f) => f.func_name === ast.func_name ); if (funcDescriptor === undefined || env.value[0]["type"] === "array") { - const arrayValue = await Promise.all(env.value.map((v) => this.resolveFunctionCall(ast, v))); + const arrayValue = await promiseOneByOne(env.value.map((v) => this.resolveFunctionCall(ast, v))); return { type: "array", diff --git a/package-lock.json b/package-lock.json index 8dcf9a9..7f56522 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "reactgenie-dsl", - "version": "0.0.41", + "version": "0.0.43", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "reactgenie-dsl", - "version": "0.0.41", + "version": "0.0.43", "license": "Apache-2.0", "dependencies": { "@babel/cli": "^7.23.0", diff --git a/package.json b/package.json index c4c2288..d63a0d2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "reactgenie-dsl", - "version": "0.0.42", + "version": "0.0.43", "description": "A natural language parser based on a large language model", "scripts": { "prepare": "peggy lib/dsl/parser.pegjs -o lib/dsl/parser.gen.js && tsc",