gpt4 book ai didi

javascript - nodejs : event. 是异步的吗?

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:07 25 4
gpt4 key购买 nike

我正在阅读这篇文章:write async function with EventEmitter

下面的代码是异步的吗?

var event = new events.EventEmitter();
event.on("done", cb);

最佳答案

在给定的相关代码中,您正在订阅一个事件。当您调用 event.emit("done") 时, Node 按照订阅的顺序执行给定的回调。

示例

    var event = new events.EventEmitter();

event.on("done",() => {
console.log(("notified 1"))
});

event.on("done",() => {
setImmediate(() => {
console.log("async")
});
console.log(("notified 2"))
});

event.on("done",async () => {
console.log(("notified 3"))
});

console.log("before firing an event");

event.emit("done");

console.log("after firing an event");

输出

before firing an event
notified 1
notified 2
notified 3
after firing an event
async

关于javascript - nodejs : event. 是异步的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46324622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com