【发布时间】:2022-01-21 20:03:58
【问题描述】:
在nodejs中为子进程附加回调或事件监听器有什么区别。喜欢-
const execute = require('child-process').exec;
const process = execute('ping -n 1 www.google.com'); // or ping -c 1 www.google.com for mac
process.stdout.on('data', data => {
console.log(data)
})
在上面的代码中,我使用事件侦听器进行输出,并且在 Windows 中获取标准输出数据,但无法在 macOS 中获取输出。但是如果我使用像 -
这样的回调const execute = require('child-process').exec;
execute('ping -c 1 www.google.com', (error, stdout, stderr) => {
console.log(stdout);
})
我在 windows 和 mac 中都获得了输出数据。使用回调或事件监听器(两者都是异步的)有什么区别吗?
【问题讨论】:
标签: javascript node.js events callback