Dealing with Java Script closure, callback and Promises

Java script is a common programming language which runs using a single thread. This does not wait until it completes the I/O operations, instead it continues the execution process which is called as non-blocking I/O. Java script is dynamic and it exists or occur at the same time which is synchronous. As a multi-paradigm language, Java script supports event-driven, functional and imperative programming styles.

When creating objects in java script, it uses ‘this’ keyword with the help of a constructor and if a function has been used with a ‘new’ keyword, that acts as a class. The code declared with a function and class both return a [prototype].

Closure

Closure is one of significant idea in JavaScript. It is broadly talked about and still confounded idea. We should comprehend what the closure is. Closure implies that an internal function consistently approaches the variables and parameters of its external function, much after the external function has returned.

We have discovered that we can make settled functions in JavaScript. Inner function can get to variables and parameters of an outer function(be that as it may, can’t get to access object of outer function).

Here, the InnerFunction() has the access to the outer variable even if it will be executed seperately.

Here, the return innerFunc returns InnerFunction from OuterFunction when it has been called the OuterFunction(). In here, the variable innerFunc has the reference to InnerFunction() only, not the OuterFunction(). If we call the innerFunc(), it can still access outerVariable that has been declared in the OuterFunction(). This is called closure.

When to use closure?

In Java Script, a closure is used to hide the implementation and encapsulate variables into a function and restrict access to it from the outside. In other words, it tends to be valuable to make private variables or functions.

CallBack

All I/O functions in Java Script is implemented to be asynchronous in nature. Since Java Script is a single threaded programming language, if an I/O function keeps holding the thread for a longer period till it finishes its execution process, Java Script will not perform better as a programming language. This why we need promises and callbacks.

Simply, callback is the term used in java script when a function starting to execute when another function has finished its execution procedure. Java script will continue executing a particular function while listening to other events as well.

Here the outputs will be 1, 2.

A problem arises when the function first() comes with another function that does not needed to be executed immediately. As a solution we can introduce a setTiomeout() function. This will delay the function execution.

Nested callbacks pass into sequence of async tasks is referred to as ‘callback hell’.

Promises

These handle the asynchronous operations in Java Script. When callbacks have created call back hell, where the code has led to an unmanageable state, these promises are used. Promises can handle multiple asynchronous operations easily providing better error handling than callbacks.

Benefits of Promises

Improved code readability

Handling asynchronous operations in a better way

Easy error handling

A promise works like a basic if-else statement. This statement can be simply understood better with the following statements.

A promise is composed with four stages.

Pending- A promise is neither fulfilled nor rejected

Fulfilled- A promise has gained its success

Rejected- A promise has failed in doing its task

Settled- A promise has fulfilled or succeeded

A promise can be created by using the following:

A promise parameter takes only one parameter, that is a callback function in Java script. But compared to promises, callback functions take two parameters; resolve and reject.

If the desired function does not go well, reject is called.

Promises can be consumed with the use of .then and .catch methods.

.then is invoked when a promise is either invoked or rejected.

.catch is used when either the code is rejected or it has some errors.

Thus, this is just an overview of some of the concepts dealt in Java Script with regard to API functions and event handling.

--

--