What is difference between Callback and Promise in Javascript with example

What is difference between Callback and Promise in Javascript with example

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 ExpertPHP!');
}, 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 like throw 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 ExpertPHP");
  }
  else {
    reject(new Error("Failure"));
  }
});
 
promise
  .then(function(res) {
    // the content from the resolve() is here - Welcome to ExpertPHP
  })
  .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.

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort serdivan escort adapazarı escort sakarya escort bayan sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sapanca escort sakarya escort