Good Bye Callbacks!
At NodeRun, we are not big fans of callbacks, especially when it comes to business logic. So, we avoid them whenever we can!
That's why all of our API are very simple to use. They accept simple parameters and provide information back as simple return values.
This makes asynchronous programming easier than ever. Consider the following line of Node.js code:
var records = pjs.query("SELECT * FROM items");
The code connects to the database to return a set of records based on an SQL statement.
It's all done with one simple line because asynchronous processing (the callback) happens within the API itself, hidden away from the developer.
So, as an application developer, you no longer have to think in terms of callbacks – you just concentrate on the business logic, and there is no "callback hell".
What about Promises and Async/Await?
While promises and the new async/await are much better than callbacks, they are not always ideal either.
The extra steps of thinking in terms of promises and deciding which functions should be made async can slow you down as your application starts to grow beyond a few lines of code. There are also instances where implementing async/await is not so trivial, like within functional array methods, such as
Using 3rd party packages and API without callbacks
3rd party package documentation will often show barebone examples that use simple callbacks.
While you may be tempted to copy/paste such examples, using callbacks will eventually lead to unnecessarily complex code, known as "callback hell".
NodeRun provides a simple way to turn any API that accepts callbacks into a simpler top-down API. Here is an example:
const fs = require('fs'); const readFile = pjs.wrap(fs.readFile); // Read a text file var text = readFile('somefile.txt');
fs.readFile(), we turn it into a top-down API using
pjs.wrap(), and now the code is simple to follow.
Can I still use Callbacks and Promises?
Yes, you can certainly still use callbacks and promises if you choose to.
Have questions about this topic? Ask for help on our NodeRun Discussion Forum.