Jobs
Blogs
Courses
Mentorship
Partners
For Employers
e2e logo

JavaScript Callback , Async , Await , Promise

mac

Callback , Async , Await , Promise


                 Callback, Promises, and Async-await are a way to deal with asynchronous data. Asynchronous programming in JavaScript is a way in which the program doesn’t wait until the execution of something is going on.


Asynchronous vs. Synchronous:

                In synchronous operations tasks are performed one at a time and only when one is completed, the following is unblocked. In other words, you need to wait for a task to finish to move to the next one. 

                In asynchronous operations, on the other hand, you can move to another task before the previous one finishes. 


1.Callbacks

                A callback is a function that is passed to another function. When the first function is done, it will run the second function.


2.Async 

             It simply allows us to write promises based code as if it was synchronous and it checks that we are not breaking the execution thread. It operates asynchronously via the event-loop. Async functions will always return a value. It makes sure that a promise is returned and if it is not returned then JavaScript automatically wraps it in a promise which is resolved with its value.


3.Await

         Await function is used to wait for the promise. It could be used within the async block only. It makes the code wait until the promise returns a result. It only makes the async block wait. 


4.Promises 

             A promise in JavaScript is similar to a promise in real life. When we make a promise in real life, it is a guarantee that we are going to do something in the future. Because promises can only be made for the future.A promise has two possible outcomes: it will either be kept when the time comes, or it won’t.

 A Promise has four states: 

         fulfilled: Action related to the promise succeeded

          rejected: Action related to the promise failed

          pending: Promise is still pending i.e. not fulfilled or rejected yet

          settled: Promise has fulfilled or rejected

Let's we see on next Blog...

 Thank you...