node.js - synchronos事件侦听器

时间:2018-05-22 13:52:17

标签: javascript node.js asynchronous

在我的javascript脚本中,我被迫使用EventEmitter并将问题分成两个函数。

有没有办法制作一个同步事件监听器?所以脚本会等到事件被触发。

const EventEmitter = require('events');
var emitter = new EventEmmitter();

function bar(arg) {
  //calculations and stuff
  emitter.emit('done', result);
}

function foo(arg) {
  bar(arg);
  emitter.on('done', (result) => {
    return result; //this return statement doesn't work for foo()
  });

  //the code here should be processed after the event above was triggered.
  //here should be a return statement for the result 

}

console.log(foo("something")); //or process the result otherwise

0 个答案:

没有答案