Are all Node.js callback functions asynchronous?
asynchronous, javascript, node.js
Solution
are all Node.js callback functions made asynchronous/non-blocking?
No. Only I/O is usually asynchronous, but many other callbacks are synchronous. Always check the docs.
Examples of async functions:
- Async Filesystem access (they have sync counterparts without callbacks, though)
- Timers (`setTimeout`)
- `process.nextTick`, `setImmediate`
- most database connections
- network connections
- Promises
Examples of sync callbacks:
- EventEmitter (depends on when the event is fired)
- Array iteration methods like `forEach`
- Array `sort` comparator callbacks
- String `replace` match callbacks
See also Are all javascript callbacks asynchronous? If not, how do I know which are? (including some other examples).
Problem
I'm working on learning Node.js and all I hear in every tutorial is "Node is asynchronous and no -blocking!" 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?