“Обещание JavaScript” Ответ

JavaScript обещает

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 */ }
);
Grepper

JS обещает

console.log('some piece of code');
examplePromise.then(function(result){
  console.log(result);
}).catch(function (error) {
  console.error(error);
});
console.log('another piece of code');
Blue Beetle

Обещание JavaScript

let promise = new Promise(function(resolve, reject){
  try{
  	resolve("works"); //if works
  } catch(err){
    reject(err); //doesn't work
  }
}).then(alert, console.log); // if doesn't work, then console.log it, if it does, then alert "works"
Unusual Unicorn

Обещание JavaScript

/*
Promise is a constructor function, so you need to use the new keyword to 
create one. It takes a function, as its argument, with two parameters - 
resolve and reject. These are methods used to determine the outcome of the
promise. 
The syntax looks like this:
*/

const myPromise = new Promise((resolve, reject) => {

});

Owlthegentleman

Обещание JavaScript

let num = 10;
//call back function
const promis = new Promise(function (resolve, reject) {

  if (num > 5) {

    //this resolve method will send data to resoleveData variable
    resolve(" Problem resolved successfully")
  }
  else {
    //this reject method will send data to rejectData variable
    reject("sorry problem couldn't solve")
  }

})
//resoleveData variable
promis.then(function (resolveData) {

  console.log(resolveData)
  //rejectData variable
}).catch(function (rejectData) {

  console.log(rejectData)
})
SkiLL3r

Обещание JavaScript

const promiseA = new Promise( (resolutionFunc,rejectionFunc) => {
    resolutionFunc(777);
});
// At this point, "promiseA" is already settled.
promiseA.then( (val) => console.log("asynchronous logging has val:",val) );
console.log("immediate logging");

// produces output in this order:
// immediate logging
// asynchronous logging has val: 777
Tender Teira

Ответы похожие на “Обещание JavaScript”

Вопросы похожие на “Обещание JavaScript”

Больше похожих ответов на “Обещание JavaScript” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования