Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why some synchronous library in nodejs stick to use callback?

Tags:

node.js

I'm a new nodejs developer, and I think that I understand designs of nodejs.

I can understand that some syscall-related library should be asynchronous. These job will be queued and processed in different thread pool, and notified via callback when it finished.

However, I can't understand why some non-syscall related libraries are using callback as a communication method. For example, xml2js library is written in Javascript, and apparently, there is no reason that this parse function will return status via callback. It can return processing result directly, and code readability will be increased.

My question is, is there any reason that some nodejs library designers are sticking to callback-based communication instead of just using return value?

like image 955
yjh0502 Avatar asked Apr 13 '11 13:04

yjh0502


People also ask

Are all callbacks asynchronous NodeJS?

The function that takes another function as an argument is called a higher-order function. According to this definition, any function can become a callback function if it is passed as an argument. Callbacks are not asynchronous by nature, but can be used for asynchronous purposes.

What is callback callback is an asynchronous equivalent for a function?

Callback is called when task get completed and is asynchronous equivalent for a function. Using Callback concept, Node. js can process a large number of requests without waiting for any function to return the result which makes Node. js highly scalable.

How does node handle callback?

js highly scalable. For example: In Node. js, when a function starts reading the file, it returns the control to the execution environment immediately to execute the next instruction. Once file I/O gets completed, the callback function will get called to avoid blocking or wait for File I/O.


2 Answers

There is nothing wrong with the callback style.

var return = doSomething();
...

doSomething(function(return) {
    ...
});

This is preference. They're both valid. The former only seems "better" because you're used to it.

One good reason for the latter is that the API doesn't have to change when you change your library to be non-blocking.

People need to be reminded that node is a low level non blocking IO library. If you're going to do something like js2xml then clearly you're waiting for node 0.6 to standardise the web worker sub process API so you can send the heavy lifting to a sub proccess and do the actual hard work in a non blocking fashion.

To do this in a non blocking manner your API needs to be asynchronous (or you need it build into the language).

like image 168
Raynos Avatar answered Nov 15 '22 06:11

Raynos


If its not asyncronous there is no reason to use a callback to pass the return value.

I've seen some libraries using callbacks instead of returns just to pass the errors back, although, in my opinion, using throws is better and cleaner.

like image 43
masylum Avatar answered Nov 15 '22 05:11

masylum