Skip to content

Commit

Permalink
Improve promise handling (one by one instead of all together)
Browse files Browse the repository at this point in the history
  • Loading branch information
valkjsaaa committed Nov 22, 2023
1 parent 19e7376 commit 65958d6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
20 changes: 14 additions & 6 deletions lib/dsl/dsl-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import {
ParamDescriptor,
} from "../dsl-descriptor";

async function promiseOneByOne(promiseList: Promise<any>[]) {
let result = [];
for (const p of promiseList) {
result.push(await p);
}
return result;
}

function parseType(value_type: string): {
is_array: boolean;
original_type: string;
Expand Down Expand Up @@ -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":
Expand All @@ -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)));
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 65958d6

Please sign in to comment.