Using Array.prototype.some method

Did you ever got in a situation where you need to find that any one of an item in a collection satisfies the condition. if yes then you want to go ahead and do some stuff. Let’s look at this from code perspective

And the probable solution any developer would try should look like below

A better approach would be to use ‘some method’ from Array.prototype

If you can observe, some method takes a callback and returns a boolean value true/false. It returns true if any element in an array satisfies the condition passed as callback.

You can find more about this method in the below link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Iterating Complex data structure

Let say we have an array of authors

we can iterate all authors using any loop or for of here, but if we modify the data structure in below format,

then you cannot iterate all authors using for loop or any other loop with ease. So we can write our own method

The problem with this approrach is we need to be aware of the method called getAllAuthors and if our data sctructure has one more genre then we need to modify the getAllAuthors function as well. This is not cool.

So, what’s the other approach is make our data structure iterable. Can we do that. Yes we can!!

An iterable is a data structure that wants to make its elements accessible to the public. It does so by implementing a method whose key is Symbol.iterator. That method is a factory for iterators. That is, it will create iterators.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Implement synchronous run for asynchronous tasks

This is a programming problem of running two asynchronous function in synchronous manner.  We have few async tasks t1, t2 and t3.

We want to implement a runSync method which takes array of these tasks, but run them synchronously.

Lets create a task array, tasks = [t2, t3, t1], and t3 should run only when t2 is finished and then run t1.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Is jQuery really dead?

Not sure. Before being declared it is dead. We need to check its pulse. We need to check why it came into existence.

The fundamental concept of jQuery is its nature of hiding complexity. I bet many of us don’t remember the exact syntax of doing an ajax call with vanilla javascript, but sure you can recall doing an ajax call using jQuery. Yeah!. You got it. jQuery is built on a design pattern also known as Facade. Hiding complexity is the core idea of facade pattern.

Let’s recall the problem jQuery solved for us and its alternative solution. First and foremost is jQuery selectors. We can use the power of CSS selectors with jQuery. Introduction of querySelctor and querySelectorAll has already eliminated the use of jQuery selectors.

Ajax call by jQuery. jQuery came up a long way from success and failure callback syntax to syntax returning promises for ajax call but now developers are more towards reactive programming so using RxJS.

Who can forget the contribution jQuery made in the field of animation? How to move stuff here and there and at what rate. CSS3 animation APIs has already fixed these things with more efficiency (CSS uses GPU not CPU).

jQuery never solved the problem of dependency injection. We were always dependent on some other libraries like RequireJS, Steal etc for this feature.

Although jQuery provided a quick solution for design, fewer lines of codes, millions of plugin, but has not provided any MVC(Model-View-Controller) structure to applications. Whereas this is the era of frameworks, where framework takes care of solving problems for us. Writing how to do is no more the pattern(imperative programming). ReactJS provides the way where we describe(declarative approach) what we want, and frameworks take care of how to do it.

In the end, you can find traces of jQuery in almost every website, but they are legacy. We should salute jQuery for its contribution before it is gone. It will live in memories of many source code with their hallmark sign of $.

Remember every $ is not jQuery. Link for this article in medium.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Fear of a Web Developer

I slept for a night just for a few hours and when I woke up there is a new JavaScript framework born. The irony is there are many new framework and library coming out who I don’t know about. I don’t know how much I need to upgrade my self. I hope all the web developer interview should focus on basics. But this is not the perfect world. Some company wants a angular guy, who know how to code in angular, that’s it.

I am a simple web developer in India, by far I have not contributed enough to any open source, not a consistent member of any community. Looks like there is a lot to learn. But what would you do, if your syllabus is increasing day by day. That’s the fear I am talking about. What I did til now is follow some good people on twitter, completed my Front End Certificate in FreeCodeCamp(which I am proud of, almost 10000 hrs of coding), created a blog where I put my day to day learning(jforjs.com), few videos in Youtube. I know, I know. This is not enough.

One thing I like about this fearful life is that I am on the continuous learning path. I started loving JavaScript. I got respect for other programming languages too. I came to know, every warrior in this battle has some skills, which I can learn from them. Some of those warriors are React Ninjas, some are JS Gurus and some are new bees with amazing learning skills. Isn’t this amazing. We have new level from Functional Programming to Dyanamic Programming, IOT to Machine Learning, and hell I know what is there more to learn.

I am glad that I choose this path, and its great to be fearful. It has more pros than cons. Find this article in medium.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Patterns with Async call : Beautiful Javascript

Callback Chain

Promise Hell

Its’ not over yet

Looks synchronous with async-await

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Implementing “throttle” function

What is a throttle function. “throttle” function returns a function which will run only once with in the time specified. This is very useful when we wish to perform some operation only once even when the function is called multiple times.
For more details, please follow the link.

In this article we are discussing how can we implement our own throttle method. The definition of our function should be

So. let’s create a boolean variable which will in closure here, and change it only when the function is called or after certain time limit.

Testing our throttle function


Thank you. Please post you comments.

Facebooktwittergoogle_plusredditpinterestlinkedinmail