使用Node.js事件模块时出现意外的同步行为。

huangapple go评论78阅读模式
英文:

Unexpected synchronous behaviour when using nodejs events module

问题

I am using Node Events module for executing my function asynchronously.

var events = require('events');
var eventEmitter = new events.EventEmitter();

eventEmitter.on('myEvent', f2);

function f1(x, y) {
    console.log('got', x, y)

    eventEmitter.emit('myEvent', x, y);
    eventEmitter.emit('myEvent', x, y);
    eventEmitter.emit('myEvent', x, y);

    console.log('done')
}

var count = 0
function f2(x, y) {
    count++;
    console.log('from f2', x, y, count)
}

f1(1, 2)

Its output is

alok@alok-HP-Laptop-14s-cf3xxx:~/tmp/test-node$ node alok.js 
got 1 2
from f2 1 2 1
from f2 1 2 2
from f2 1 2 3
done

My expected output is

got 1 2
done
from f2 1 2 1
from f2 1 2 2
from f2 1 2 3

Why console.log('done') is running in last. or Why execution is synchronous?

英文:

I am using Node Events module for executing my function asynchronously.

var events = require('events');
var eventEmitter = new events.EventEmitter();

eventEmitter.on('myEvent', f2);

function f1(x, y) {
    console.log('got', x, y)

    eventEmitter.emit('myEvent', x, y);
    eventEmitter.emit('myEvent', x, y);
    eventEmitter.emit('myEvent', x, y);

    console.log('done')
}

var count = 0
function f2(x, y) {
    count++;
    console.log('from f2', x, y, count)
}

f1(1, 2)

Its output is

alok@alok-HP-Laptop-14s-cf3xxx:~/tmp/test-node$ node alok.js 
got 1 2
from f2 1 2 1
from f2 1 2 2
from f2 1 2 3
done

My expected output is

got 1 2
done
from f2 1 2 1
from f2 1 2 2
from f2 1 2 3

Why console.log('done') is running in last. or Why execution is synchronous?

答案1

得分: 1

以下是翻译好的内容:

因为这是它的工作方式:

> 当 EventEmitter 对象触发事件时,所有附加到该特定事件的函数都会同步调用。
>
> [..]
>
> ## 异步 vs. 同步
>
> EventEmitter 以它们注册的顺序同步调用所有监听器。这确保了事件的正确排序,有助于避免竞争条件和逻辑错误。在适当的情况下,监听器函数可以使用 setImmediate()process.nextTick() 方法切换到异步操作模式:

>
> const myEmitter = new MyEmitter();
> myEmitter.on('event', (a, b) => {
> setImmediate(() => {
> console.log('this happens asynchronously');
> });
> });
> myEmitter.emit('event', 'a', 'b');
>
> https://nodejs.dev/en/api/v19/events

英文:

Because that's how it works:

> When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously.
>
> [..]
>
> ## Asynchronous vs. synchronous
>
> The EventEmitter calls all listeners synchronously in the order in which they were registered. This ensures the proper sequencing of events and helps avoid race conditions and logic errors. When appropriate, listener functions can switch to an asynchronous mode of operation using the setImmediate() or process.nextTick() methods:
>
> const myEmitter = new MyEmitter();
> myEmitter.on('event', (a, b) => {
> setImmediate(() => {
> console.log('this happens asynchronously');
> });
> });
> myEmitter.emit('event', 'a', 'b');
>
> https://nodejs.dev/en/api/v19/events

huangapple
  • 本文由 发表于 2023年1月3日 18:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/74991959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定