A promise is in one of three different states:
- pending — The initial state of a promise.
- fulfilled — The state of a promise representing a successful operation.
- rejected — The state of a promise representing a failed operation.
Let's take an example:
First, we will look into the example which gets resolved,
Here, we create two-parameters inside the object of
. We take
as a promise and check whether it's fulfilled or not. Line 11, we create
which will call the message inside
. If we run the code then the condition is true because “promise a” value will be true i.e ‘a value equals to 2'
. Since the condition gets satisfied We will get the message from ‘resolve’.
Another example which shows promise not being resolved,
Here, we create two-parameters inside the object of Promise(resolve, reject). We take
as a promise and check whether it’s fulfilled or not. Line 11, we create
which will call the message inside
. If we run the code then the condition is false because “promise a” value will not be true i.e ‘a value not equals to 2’
. Since the condition gets satisfied We will get the message from ‘reject’.
A callback is a function called at the completion of a given task; this prevents any blocking and allows other code to be run in the meantime. Callbacks are the foundation of Node.js. Callbacks give you an interface with which to say, “and when you’re done doing that, do all this.”
This allows you to have as many IO operations as your OS can handle happening at the same time. For example, in a web server with hundreds or thousands of pending requests with multiple blocking queries, performing the blocking queries asynchronously gives you the ability to be able to continue working and not just sit still and wait until the blocking operations come back.
Example of Callbacks:
Here, We are using a very simple callback function which takes two callbacks i.e one for success and another is for error. Here the function checks whether two variables are true or not. If it's true then the callback will be for
message else it will call
Now we will take the same example using promises:
Both the example looks the same except some syntax. But writing code is cleaner with Promises than with callbacks because if you start using nesting callbacks we might get in trouble as the code just keeps getting indented and indented.
But in Promises, You can create
which is better than using multiple callbacks i.e callbacks inside of another and another or nesting callback also called callback hell. So, Promises are great and solve the inside looping problem with callbacks.
let's look at the following Example with Promises and compare using Async/Await,
The above code is similar to the codes which are shown earlier. Two functions, One for sending a request and another for the response to that request.
Now we will see the same code with Async/Await and see some differences,
Here, we see a lot of differences in the code which also looks cleaner than code written with only ‘Promise’. We must wrap our code inside a function with an
keyword before the function definition. Inside that function, code will be written in the
will take the code which might throw an error and
will send an error message if any error is caught. We must use await keyword before the functions that are going to be asynchronous. Otherwise, it will just return the promise but not the result of that promise being executed. Other than that Async/Await is pretty much similar to Promises. It makes easier for us to write promises and work with promises in our code.
You can further refer to the below links to learn more about Promises, Callbacks, and Async/Await…
Subscribe to get your daily round-up of top tech stories!