Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all Node.js callback functions asynchronous?

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?

like image 250
Kris Avatar asked Feb 19 '14 15:02

Kris


1 Answers

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).

like image 159
Bergi Avatar answered Oct 04 '22 19:10

Bergi