What is difference between Callback and Promise in Javascript with example
In this post we will give you information about What is difference between Callback and Promise in Javascript with example. Hear we will give you detail about What is difference between Callback and Promise in Javascript with exampleAnd how to use it also give you demo for it if it is necessary.
What is difference between Callback and Promise with example
In this Javascript Tutorial, you will learn the basic difference between Callback and Promise with an example.
If you are going to start your career as a Node js developer then you must know about the callback, promises and use of Async and Await to handle deferred operations in JavaScript.
Javascript is a powerful programming language with its ability for closure, callback functions and many other features.
In Javascript, Callback and Promise are two main methods that handle asynchronous tasks.
Callback:
A callback is a function that is passed to an another function and also known as a higher-order function. In another words, when any function accepts another function as an argument then this contained function is called callback function.
Due to event-driven non-blocking I/O model, Node is heavy use of callbacks. A callback may or may not be executed asynchronously.
Example 1:
setInterval(function() { console.log('Welcome to itsolutionstuck!'); }, 1000);
setInterval
accepts a callback function as its first parameter and also a time interval.
Example 2:
function add(val) { return val+1; } function display(callback) { return callback(2); } console.log(display(add)); // Result: 3
In the above example, add()
is a function and it is passed to function display()
as a callback.
Promise:
A promise is an object which is used to handle the asynchronous result of an operation.
The promise constructor takes one argument, a callback function with two parameters, resolve and reject.
let promise =new Promise(function(resolve, reject) { // promise description });
Its arguments resolve and reject are callbacks provided by JavaScript itself. Our code is only inside the callback function.
-
resolve()
— if everything worked successfully then call resolve with result value.
-
reject(error)
— if an error occurred then call reject with an error object likethrow
in plain Javascript.
Example 1:
var flag=true var promise =new Promise(function(resolve, reject) { // do a thing, possibly async, then… if (flag) { /* everything worked successfully */ resolve("Welcome to itsolutionstuck"); } else { reject(newError("Failure")); } }); promise .then(function(res) { // the content from the resolve() is here - Welcome to itsolutionstuck }) .catch(function(err) { // the error object from the reject() is here - Failure });
As compared to Callback function, It is very easy to debug the error with Promise like try-catch block.
A promise made the chaining of functions straightforward and simplified the code. Common need of chaining is to execute two or more asynchronous operations back to back, making it much easier to read. Each subsequent operation starts with the result from the previous step when the previous operation succeeds.
Promises are built over callbacks, it is much more readable then Callback function code because The truth is, making use of callbacks in a clean and concise way is challenging.
Hope this code and post will helped you for implement What is difference between Callback and Promise in Javascript with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs