What is the promises is Javascript ?

how can we use promise instead of call back, what is the exactly concept of promises


9 Answers
1-9 of  9
9 Answers
  • A Promise in JavaScript is an object that represents the future result of an asynchronous operation.
    It can be:
    resolved: success
    rejected: error
    pending: still waiting
    Example:
    JavaScript
    let p = new Promise((resolve, reject) => {
      resolve("Done");
    });
    Short definition for exam:
    A Promise is used in JavaScript to handle asynchronous work and its result later.

  • In JavaScript, a Promise is an object that represents the result of an asynchronous operation—something that may finish now, later, or fail.
    🔹 Simple Meaning
    A Promise is like a guarantee:
    “I will give you a result later — either success or failure.”
    🔹 Why Promises are Used
    JavaScript often runs tasks like:
    Fetching data from a server 🌐
    Reading files 📂
    Waiting for a timer ⏱️
    These take time, so Promises help handle them without blocking the program.
    🔹 States of a Promise
    A Promise has 3 states:
    Pending → initial state (waiting)
    Fulfilled → operation successful
    Rejected → operation failed
    🔹 Basic Syntax
    JavaScript
    let promise = new Promise(function(resolve, reject) {
        let success = true;
     
        if(success) {
            resolve("Task completed!");
        } else {
            reject("Task failed!");
        }
    });
    🔹 Using a Promise
    JavaScript
    promise
      .then(function(result) {
          console.log(result); // if success
      })
      .catch(function(error) {
          console.log(error); // if error
      });
    🔹 Real Example
    JavaScript
    function getData() {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve("Data received");
            }, 2000);
        });
    }
     
    getData().then(data => console.log(data));
    👉 Output after 2 seconds:
    Data received
    🔹 Promise Methods
    .then() → runs when successful
    .catch() → runs when error occurs
    .finally() → runs always

  • A Promise in JavaScript is an object used to handle asynchronous operations, meaning tasks that take time to complete like fetching data from a server or reading a file. It represents a value that may be available now, later, or never, and it has three states: pending (still in progress), fulfilled (completed successfully), and rejected (failed). Promises help avoid deeply nested callbacks and make code cleaner and easier to manage by using methods like .then() for success and .catch() for errors.

  • JavaScript, a Promise is an object that represents the result of an asynchronous operation (something that takes time, like loading data from a server).
    Instead of getting the result immediately, a Promise gives you a placeholder that will be completed later—either successfully or with an error.
    A Promise has three states:
    Pending – the operation is still in progress
    Fulfilled (Resolved) – the operation completed successfully
    Rejected – the operation failed
    For example, when you request data from an API, JavaScript doesn’t wait and freeze your program. It continues running, and when the data is ready, the Promise gives you the result.
    Here is a simple example:
    JavaScript
    let myPromise = new Promise(function(resolve, reject) {
        let success = true;
     
        if (success) {
            resolve("Task completed");
        } else {
            reject("Task failed");
        }
    });
     
    myPromise
    .then(function(result) {
        console.log(result);
    })
    .catch(function(error) {
        console.log(error);
    });

  • A JavaScript Promise is a special object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. Instead of immediate return values, asynchronous methods return a "promise" to supply the value at some point in the future

  • ; The promise constructor takes one argument, a callback with two parameters, resolve and reject. Do something within the callback, perhaps async, then call resolve if everything worked, otherwise call reject.

  • asyncOprtn(para) .then(function(result)
    {
           // Result
    }) .
    catch(function(error)
    {
           // Error
    });
    Note here that “asyncOprtn(param)” is not a Promise itself but a function that returns a Promise.

    A promise is an object that wraps an asynchronous operation and notifies when it’s done.This sounds exactly like callbacks, but the important differences are in the usage of Promises. The methods a promise provides are “then(…)” for when a successful result is available and “catch(…)” for when something went wrong as used in above code.

  • Promise  is used for asynchronous(events occurning  at unknown time) computations.Promise is a special object which link between producing code and consuming code.consuming code has to wait for the result of producing code.Producing code takes its  time for execution. Function passed to promise is called executer(function(resolve,reject)).resolve and reject are predefined function created by javascript Engine(chrome has V8 engine). Executer runs automatically when promise is created.When the executor finishes the job, it should call one of the functions that it gets as argument.
    var r=new Promise(function(resolve,reject){
    //function(resolve,reject) is an executer also called producing code
    resolve(2);
    });
    //result of executer will be stored in r i.e 2
    //then parameter of a i.e value will be equal to result of executer(2)
    r.then(a).then(b).then(c).then(function(response){
    consle.log(response);
    })

    function a(value)
    {
    return value+2;
    }
    function b(value)
    {
    return value+2;
    }

    function c(value)
    {
    return value+2;
    }

Javascript

Didn't get the answer.
Contact people of Talent-Javascript directly by clicking here