Process faster with Neo-async Nodejs

Hey guys,

This is a twist from the normal frontend tutorial which I would have normally written, this happened because of my ability to always want to know more and it has helped me so far.

So basically this tutorial is to compare Neo-async and your normally asynchronous calls and to show which is faster and more reliable.

Neo-Async is thought to be used as a drop-in replacement for Async, it almost fully covers its functionality and runs faster. — Neo-async

Neo-async has multiple functions which are all great but my favorites are Waterfall, Auto and Parallel.

Let’s dive right in.

To use Neo-async you will have to install it

npm install --save neo-async

then using it in a project is pretty easy all you need to do is to import it into the file you need it using:

var async = require('neo-async');

now by doing the above you have access to all the neo-async functions.

For example,

Auto:

var async = require('neo-async');
let user, compared;
async.auto({
users: async (callback) => {
try {
user = await Users.findOne({ email: data.email });
console.log(user);
if (user.length < 1) {
return ResponseService.json(400, res, "Invalid login details")
} else {
callback(null, user);
}
} catch (err) {
return ResponseService.json(400, res, "Invalid login details")
}
},
comparePassword: ['users', async (results, callback) => {
console.log(results);
try {
compared = await Users.comparePassword(data.password, results.users);
console.log(compared)
if (!compared) {
return ResponseService.json(400, res, "Invalid login details")
} else {
callback(null, compared);
}
} catch (err) {
return ResponseService.json(403, res, "Forbidden")
}
}],
tokenGenerator: ['users', 'comparePassword', (results, callback) => {
const token = TokenService.generateToken(user);
const sql = "UPDATE `users` SET `access_token` = ?, `last_logged_in` = ?, `ipaddress` = ? WHERE `email` = ?";
Users.query(sql, [token, new Date(), requestIp.getClientIp(req), user.email], (err, updatedUser) => {
if (err) {
console.log(err);
callback(err);
} else {
console.log(updatedUser);
callback(null, updatedUser);
}
});
}]
}, (err, results) => {
if (err) {
return ResponseService.json(400, res, "Something went wrong");
} else {
return ResponseService.json(200, res, "Successfully signed in", results.tokenGenerator[0]);
}
});

The above code is a basic login process which involves checking if an email exist then checking if the hashed password is valid then generating a token for the login session and updating the db with the access token and last logged in of the user.

To do the above with out neo-async or async you would have to have nested codes (that is code inside code) which sometimes can be messy and slow.

But neo-async beautified and made the process faster.

Below is a pictorial view of the difference in the response time between the neo-async and normal nested async nested codes.

The above pictures show the difference in the response time between having neo-async and not having neo-async . We will notice that the one with neo-async (the one on the left) has a response time of 1.53s which the one without it has a response time of 2.67s . This may not seem like alot but when a request is sent to the backend the delivery time of the response greatly affects the user experience.

Imagine using without caching data and it is still fast, imagine what will happen when you cache your data and use neo-async at the same time. It is mind blowing

Thank you for reading.