JavaScript: Get Chained!

omid habibi
2 min readDec 1, 2017

--

You have done everything you could, Your functions just do the one thing that their name says and they don’t make any changes to passed inputs. You have put a lot of effort into this project, Stayed up all night just reduce the number of lines of a function, Used all your willpower to resist the urge of not writing unit tests and deeply thought about the commit messages.

You have kept every part of your code consistently clean because you knew that even a little rush or laziness could make your project to end up as an unreadable project or decrease its maintainability.

Everything is done. You just have to document your API endpoint so your users be able to communicate with each other.

const allUsers = getUsers();const receivers = allUsers.filter('friends');
const message = "Hello Everyone!";
await send(message, receivers);
closeConnection();

It works! But not satisfying.

send(message).to('all').except('friends').then.closeConnection; 

Much better! how hard can it be? Just return a function that returns a partial function… But wait! The order of chained methods is fluent but not practical, When the process reaches except the message has already been sent to everyone so the order must be changed into something like this:

to('all').except('friends').send(message).then.closeConnection;

Hmm… Not bad that much! Okay, But original send function is asynchronous so the connection will be closed just before sending the message. So:

to('all').except('friends').send(message).then(() => closeConnection());

Now it doesn’t worth all the trouble of creating chained methods.

The problem described above made me to think, Why can’t I call my methods in any order I want? So to solve my issue I decided to make my own tool. ClarifyJs made to make and priorities chained methods so the mentioned desired sample could be made like this:

const routes = [
{
path: '',
handler: sendMessageFunc,
inject: ['contacts'],
priority: 0
},
{
path: 'to',
handler: selectContactFunc,
storeResultAs: 'contacts',
priority: 2
},
{
path: 'to.except',
handler: filterContactFunc,
inject: ['contacts'],
storeResultAs: 'contacts',
priority: 1
},
{
path: 'to.except.then.closeConnection',
handler: closeConnectionFunc,
priority: -Infinity
}
];
const send = clarify({ routes });

This made possible by making the whole process asynchronous. Returned promise resolves with the result of the method with least priority.

Also ClarifyJs is integrated with a storage to store returned results from other methods and pass them to other methods if necessary.

After sometime I noticed they are times when I want to wait for a specific asynchronou smethod to complete before moving on so I added the feature to tell the clarify to wait for a asynchronous method if necessary just to solve closeConnection issue.

So sendMessage route becomes:

{
path: '',
handler: sendMessageFunc,
inject: ['contacts'],
+awaitForHandler: true,
priority: 0
}

I hope this library helps to decrease complexities of creating chaining methods and encourage fluent API.

--

--