“Obietnice JavaScript” Kod odpowiedzi

Obietnice 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 obietnice

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

Obietnica JavaScript

let promise = new Promise((resolve,reject)=>{
                try {
                    resolve("some data");
                } catch (error) {
                    reject(error);
                }
            })
            
            promise.then(function (data) {
                console.log(data);
            },function (error) {
                console.error(error);
            })
Powerful Pigeon

Obietnice JavaScript

var posts = [
  {name:"Mark42",message:"Nice to meet you"},
  {name:"Veronica",message:"I'm everywhere"}
];

function Create_Post(){
  setTimeout(() => {
    posts.forEach((item) => {
      console.log(`${item.name} --- ${item.message}`);
    });
  },1000);
}

function New_Post(add_new_data){
  return new Promise((resolve, reject) => {
    setTimeout(() => {
     posts.push(add_new_data);
      var error = false;
      if(error){
        reject("Something wrong in </>, Try setting me TRUE and check in console");
      }
      else{
        resolve();
      }
    },2000);
  })
}

New_Post({name:"War Machine",message:"I'm here to protect"})
    .then(Create_Post)
    .catch(err => console.log(err));
Outlander

Obietnice JavaScript

// returns a promise
let countValue = new Promise(function (resolve, reject) {
   reject('Promise rejected'); 
});

// executes when promise is resolved successfully
countValue.then(
    function successValue(result) {
        console.log(result); // Promise resolved
    },
 )
SAMER SAEID

Obietnice JavaScript

let dataIsLoading = true;

promise
  .then(data => {
    // do something with data
  })
  .catch(error => {
   // do something with error
  })
  .finally(() => {
    dataIsLoading = false;
  })
Gifted Goat

Odpowiedzi podobne do “Obietnice JavaScript”

Pytania podobne do “Obietnice JavaScript”

Więcej pokrewnych odpowiedzi na “Obietnice JavaScript” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu