Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Rule chaining not working with Promise.all

未关闭
#303 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
javascript
领域
backend

调研方向

首先复现 issue 中的 Promise.all 示例,然后检查 Engine.run 以及 almanac.addRuntimeFact 使用的 runtime-fact 处理方式。当并行运行针对 samuel、joseph 和 anthony 产生相应的结果和事件,并与顺序运行的行为一致时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

If I run the engine sequentially, I am getting the desired result. If I run it in parallel, I am getting results only for the first run.

student-fav-subject.js

const { Engine } = require("json-rules-engine");

const apiClient = require("./student-api-client");

let engine = new Engine();

async function start() {
  let mathematicsSubjectRule = {
    conditions: {
      all: [
        {
          fact: "student-information",
          operator: "contains",
          value: "Mathematics",
          path: "$.subjects",
        },
      ],
    },
    event: { type: "studies-mathematics" },
    priority: 10,
    onSuccess: async function (event, almanac) {
      almanac.addRuntimeFact("studiesMathematics", true);
    },
    onFailure: function (event, almanac) {
      almanac.addRuntimeFact("studiesMathematics", false);
    },
  };
  engine.addRule(mathematicsSubjectRule);

  let physicsFavoriteRule = {
    conditions: {
      all: [
        {
          fact: "studiesMathematics",
          operator: "equal",
          value: true,
        },
        {
          fact: "student-information",
          operator: "equal",
          value: "Physics",
          path: "$.favoriteSubject",
        },
      ],
    },
    event: {
      type: "physics-favorite-subject",
      params: {
        message: " has Physics as their favorite subject",
      },
    },
  };

  engine.addRule(physicsFavoriteRule);

  engine.addFact("student-information", function (params, almanac) {
    console.log("loading student information...");
    return almanac.factValue("studentName").then((studentName) => {
      return apiClient.getStudentInformation(studentName);
    });
  });

  engine.on("success", (event, almanac, ruleResult) => {
    almanac.factValue("studentName").then((studentName) => {
      switch (event.type) {
        case "physics-favorite-subject":
          console.log(
            studentName,
            "HAS".green,
            "Physics as their favorite subject"
          );
      }
    });
  });

  engine.on("failure", (event, almanac, ruleResult) => {
    almanac.factValue("studentName").then((studentName) => {
      switch (event.type) {
        case "physics-favorite-subject":
          const studiesMathResult = ruleResult.conditions.all.find(
            (condition) => condition.fact === "studiesMathematics"
          );
          if (studiesMathResult) {
            if (studiesMathResult.result) {
              console.log(
                studentName,
                "DOES NOT".red,
                "have Physics as favorite subject"
              );
            } else {
              console.log(
                studentName,
                "CANNOT NOT".red,
                "have Physics as favorite subject as they do not study Mathematics"
              );
            }
          }
      }
    });
  });

  Promise.all([
    engine.run({ studentName: "samuel" }),
    engine.run({ studentName: "joseph" }),
    engine.run({ studentName: "anthony" }),
  ]);

  // await engine.run({ studentName: "samuel" });
  // await engine.run({ studentName: "joseph" });
  // await engine.run({ studentName: "anthony" });
}

start();

student-api-client.js

"use strict";

require("colors");
const fs = require("fs");

let studentData = require("./student-data.json");

/**
 * mock api client for retrieving student information
 */
module.exports = {
  getStudentInformation: (studentName) => {
    const message = 'loading student information for "' + studentName + '"';
    console.log(message.dim);
    return new Promise((resolve, reject) => {
      setImmediate(() => {
        resolve(studentData[studentName]);
      });
    });
  },
};


student-data.json

{
  "john": {
    "favoriteSubject": "Mathematics",
    "subjects": ["Physics", "Mathematics"]
  },
  "anthony": {
    "favoriteSubject": "Physics",
    "subjects": ["Physics", "Mathematics"]
  },
  "george": {
    "favoriteSubject": "History",
    "subjects": ["History", "Mathematics"]
  },
  "samuel": {
    "favoriteSubject": "Physics",
    "subjects": ["Physics", "History", "Geography", "Chemistry"]
  },
  "joseph": {
    "favoriteSubject": "Economics",
    "subjects": ["Economics", "Mathematics", "History"]
  }
}

Output

Running sequentially
loading student information...
loading student information for "samuel"
samuel CANNOT NOT have Physics as favorite subject as they do not study Mathematics
loading student information...
loading student information for "joseph"
joseph DOES NOT have Physics as favorite subject
loading student information...
loading student information for "anthony"
anthony HAS Physics as their favorite subject
Running parallelly (Promise.all)
loading student information...
loading student information...
loading student information...
loading student information for "samuel"
loading student information for "joseph"
loading student information for "anthony"
samuel CANNOT NOT have Physics as favorite subject as they do not study Mathematics
主要语言
JavaScript
星标
3.1k
派生
507
PR 合并指标
30 天内没有已合并 PR

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

CacheControl/json-rules-engine 的其他 Issue

查看 CacheControl/json-rules-engine 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。