Javascript 在节点js中等待事件

Javascript 在节点js中等待事件,javascript,node.js,events,npm,bpmn,Javascript,Node.js,Events,Npm,Bpmn,如何在node js中等待事件?我正在开发一个bpmn工作流,我必须一步一步地执行事件。服务器由几个脚本组成,每个脚本都是一个事件,如下所示: 'use strict'; const Bpmn = require('bpmn-engine'); const processXml = ` <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/

如何在node js中等待事件?我正在开发一个bpmn工作流,我必须一步一步地执行事件。服务器由几个脚本组成,每个脚本都是一个事件,如下所示:

'use strict';
const Bpmn = require('bpmn-engine');
const processXml = `
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <process id="theProcess" isExecutable="true">
    <startEvent id="start" />
    <exclusiveGateway id="decision" />
    <endEvent id="RFID_ERRATO" />
    <endEvent id="RFID=M1" />
    <sequenceFlow id="flow1" sourceRef="start" targetRef="decision" />
    <sequenceFlow id="flow2" sourceRef="decision" targetRef="RFID_ERRATO">
      <conditionExpression xsi:type="tFormalExpression" 
language="JavaScript"><![CDATA[
      this.variables.input != "M1"
      ]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="decision" targetRef="RFID=M1">
      <conditionExpression xsi:type="tFormalExpression" 
language="JavaScript"><![CDATA[
      this.variables.input = "M1" 
      ]]></conditionExpression>
    </sequenceFlow>
  </process>
</definitions>`;

const engine = new Bpmn.Engine({
  name: 'exclusive gateway example1',
  source: processXml
});

engine.once('end', (definition) => {
  if (definition.getChildActivityById('RFID_ERRATO').taken) throw new 
Error('<RFID_ERRATO> was not supposed to be taken, check your input');
  console.log('TAKEN RFID=M1', 
definition.getChildActivityById('RFID=M1').taken);
});


function sendEvent(value){
  engine.execute({
  variables: {
    input: value
  }
}, (err, definition) => {
  console.log(engine.getState())
});
}

var i = 0;
//hello.js
module.exports = (req, res, next) => {
  //res.header('X-Hello', 'World')
  //console.log(req);
  if(!i++){
    sendEvent(req.body.rfid);
  }
  console.log(engine.getState())
  next()
}
“严格使用”;
const Bpmn=require('Bpmn-engine');
常量processXml=`
`;
常量引擎=新的Bpmn.引擎({
名称:“独占网关示例1”,
来源:processXml
});
引擎。一次('结束',(定义)=>{
if(definition.getChildActivityById('RFID\u ERRATO').take)抛出新
错误('不应该被采取,检查您的输入');
console.log('take RFID=M1',
定义.getChildActivityById('RFID=M1').take);
});
函数sendEvent(值){
引擎执行({
变量:{
输入:值
}
},(错误,定义)=>{
console.log(engine.getState())
});
}
var i=0;
//你好
module.exports=(请求、恢复、下一步)=>{
//res.header('X-Hello','World')
//控制台日志(req);
如果(!i++){
sendEvent(请求主体rfid);
}
console.log(engine.getState())
下一个()
}

(我正在使用这些模块)。服务器开始在命令行“json server db.json--middleware./script1.js./script2.js”上写入数据,然后我调用请求post通过服务器发送数据,仅一次。问题是,所有事件只在第一个请求时按顺序回复。我希望第一个脚本/事件在第一个请求时回复,而第二个事件正在等待,当第二个请求被发送时,下面的脚本执行它,依此类推。有可能吗?

等待后再做一些事情,您需要以异步方式运行代码,有很多很好的方法可以做到这一点

最常见的是promise,promise将从异步代码中获得返回或错误。基本示例(来自):

异步中的“事情”是我们将做一些事情,然后我们将做一些事情,这然后正在做我们在同步代码中需要和不需要的事情

有很多npm包也可以帮助我们做到这一点,比如,它将以系列的形式运行函数,例如来自github的函数:

/* basic - no arguments */
waterfall(myArray.map(function (arrayItem) {
  return function (nextCallback) {
    // same execution for each item, call the next one when done
    doAsyncThingsWith(arrayItem, nextCallback);
}}));

/* with arguments, initializer function, and final callback */
waterfall([function initializer (firstMapFunction) {
    firstMapFunction(null, initialValue);
  }].concat(myArray.map(function (arrayItem) {
    return function (lastItemResult, nextCallback) {
      // same execution for each item in the array
      var itemResult = doThingsWith(arrayItem, lastItemResult);
      // results carried along from each to the next
      nextCallback(null, itemResult);
}})), function (err, finalResult) {
  // final callback
});
它将运行一系列函数的Array.map,避免了我们在使用异步代码时遇到的一个好的敌人


因此,async代码将允许您等待事件,因为它让您做一些事情,然后对结果做另一件事情。

我相信有几种方法可以做到这一点。我想到的一个方法是用
串起承诺。然后
让每个请求只在最后一个请求发出后发出。在很多方面,这与语言的主要优点背道而驰。也许您精通的另一种语言(如Python)更适合于此?抱歉,我不精通其他语言。为什么这种方式与该语言的主要优点背道而驰?Node使用单线程事件循环,在其中发送要完成的事情,然后就可以处理下一件事情。如果每次发送请求时都要求节点阻止,则最终会失去事件循环结构的好处。尽管如此,这是可以做到的。我对Bpmn不太熟悉,但研究前景看好。您应该能够通过将下一个调用放入上一个调用的
。然后放入上一个调用的
,来创建承诺链,以使它们按顺序发生。德国劳埃德船级社
/* basic - no arguments */
waterfall(myArray.map(function (arrayItem) {
  return function (nextCallback) {
    // same execution for each item, call the next one when done
    doAsyncThingsWith(arrayItem, nextCallback);
}}));

/* with arguments, initializer function, and final callback */
waterfall([function initializer (firstMapFunction) {
    firstMapFunction(null, initialValue);
  }].concat(myArray.map(function (arrayItem) {
    return function (lastItemResult, nextCallback) {
      // same execution for each item in the array
      var itemResult = doThingsWith(arrayItem, lastItemResult);
      // results carried along from each to the next
      nextCallback(null, itemResult);
}})), function (err, finalResult) {
  // final callback
});