Asynchronous Programming
What is Asynchronous?
In general, asynchronous comes from Greek asyn-, meaning “not with,” and chronos, meaning “time” is an adjective describing objects or events that are not coordinated in time.
In computer programs, asynchronous operation means that a process operates independently of other processes, whereas synchronous operation means that the process runs only as a result of some other process being completed or handing off operation.
In a synchronous way the first operation is start first and complete its execution then the next operation is start after complete the first one. Next operation is blocked by the previous operation until complete it. But in asynchronous programming one task never wait for another task for execute. The task execute first which completed first. It never block one task input or output for another task.
JavaScript is an asynchronous programming language and java is a synchronous language.
Example 1: Synchronous programming java
In this example after execute first line it will wait for 5 sec, then second and third line will be executed. Also wait 4 second after third line and execute fourth and fifth line.
Example 2: Asynchronous programming JavaScript.
In this example using JavaScript no line wait for another line. Here second line does not wait for first line and forth line does not wait after third line. These all lines are execute at run time. No line blocked another line. That is non-blocking of asynchronous programming.
There are different way to implement Asynchronous in JavaScript.
- Callback function
- Promise
- Generators
- Async & Await
I will describe here about Callback function and Promise.
Callback function:
Callback function is a function that return or use another function as it’s parameter.
Callback is an easier way to describe asynchronous programming.
Example:
In this example setTimeout function uses another function to display second line. And this timeout function set 5 sec waiting time for second line and 4 sec for fourth line.
But in execution third and fifth line does not wait for second and fourth line. They are executed after first line. Then fourth line is executed before second line because second line needs more time.
Here second line need 5 sec . But it can’t block the execution of another line after it. So this is an non-blocking process and this process is defined by asynchronous.
Promise:
Promise is a build in object in JavaScript(ES6).
Promises are the way to deal with asynchronous programming without writing too many callback in function.
It is begin difficult to maintain the program when the chain of callback functions is so long to implement lots of asynchronous task. To solve this problem we can use promise. It is a clean way to implement asynchronous programming in JavaScript.
Promises give us a way to handle asynchronous processing in a synchronous way. They represent a value that we can handle at some point in the future. And, better than callbacks here, Promises give us guarantees about that future value, specifically:
- The promise is immutable: No other registered handlers of that value can change it.
- We are guaranteed to receive the value, regardless of when we register a handler for it, even if it’s already resolved.
For example:
Creating promise:
To create an object we can use class in ES6. We can also use a constructor with it. Constructor is used to give property values in object creation time. To deal with promise there are two methods. Then() and catch(). That works as chain
In this way a promise has three steps.
1.Pending: until a Promise is fulfilled it is in pending state
- Fulfillment: when the first handler is called the Promise is considered fulfilled with the value passed to that handler.
- Rejected: if the second handler is called, the Promise is considered rejected with the value passed to that handler.
The standard way to create a promise is to use new Promise construction with accept a handler that is given two function as parameter. The first handler is resolve and another is reject.
Example:
The then() method takes a function that will be passed the resolved value of the Promise when it is fulfilled. And the catch() method takes a single handler to be called when a promise is rejected.
Md. Hamidur Rahaman