所有的 Node.js 回调函数都是异步的吗? [英] Are all Node.js callback functions asynchronous?
问题描述
我正在学习 Node.js,在每个教程中我听到的都是Node 是异步的,没有阻塞!"
I'm working on learning Node.js and all I hear in every tutorial is "Node is asynchronous and no -blocking!"
我听说在常规浏览器 JavaScript 中,只有某些事情(例如 AJAX 调用)可以异步或非阻塞(使用回调)... Node.js 也是如此,还是所有 Node.js 回调都是如此函数实现异步/非阻塞?
I've heard in regular browser JavaScript only certain things such as AJAX calls can be made asynchronous or non-blocking (using callbacks)... Is this true of Node.js as well, or are all Node.js callback functions made asynchronous/non-blocking?
推荐答案
所有的 Node.js 回调函数都是异步/非阻塞的吗?
are all Node.js callback functions made asynchronous/non-blocking?
没有.通常只有 I/O 是异步的,但许多其他回调是同步的.始终检查文档.
No. Only I/O is usually asynchronous, but many other callbacks are synchronous. Always check the docs.
异步函数示例:
- Async Filesystem access (they have sync counterparts without callbacks, though)
- Timers (
setTimeout
) process.nextTick
,setImmediate
- most database connections
- network connections
- Promises
同步回调示例:
- EventEmitter(取决于事件何时触发)
- 数组迭代等方法
forEach
- Array
sort
比较器回调 - String
replace
匹配回调
- EventEmitter (depends on when the event is fired)
- Array iteration methods like
forEach
- Array
sort
comparator callbacks - String
replace
match callbacks
另见所有的javascript回调都是异步的吗?如果不是,我怎么知道哪些是?(包括其他一些示例).
See also Are all javascript callbacks asynchronous? If not, how do I know which are? (including some other examples).
这篇关于所有的 Node.js 回调函数都是异步的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!