Answers for "what is use of promise in javascript?"

19

javascript promises

var promise = new Promise(function(resolve, reject) {
  // do some long running async thing…
  
  if (/* everything turned out fine */) {
    resolve("Stuff worked!");
  }
  else {
    reject(Error("It broke"));
  }
});

//usage
promise.then(
  function(result) { /* handle a successful result */ },
  function(error) { /* handle an error */ }
);
Posted by: Guest on July-01-2019
2

what is a promise

// Promise is a special type of object that helps you work with asynchronous operations.
// Many functions will return a promise to you in situations where the value cannot be retrieved immediately.

const userCount = getUserCount();
console.log(userCount); // Promise {<pending>}

// In this case, getUserCount is the function that returns a Promise. If we try to immediately display the value of the userCount variable, we get something like Promise {<pending>}.
// This will happen because there is no data yet and we need to wait for it.
Posted by: Guest on February-11-2021
1

promise definition in javascript

A promise is an object that may produce a single value sometime in the future: either a resolved value, or a reason that it's not resolved (e.g., a network error occurred)
Posted by: Guest on June-24-2021

Code answers related to "what is use of promise in javascript?"

Code answers related to "Javascript"

Browse Popular Code Answers by Language