“JS обещательный пример” Ответ

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

JavaScript обещание пример базового

const anotherPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('this is the eventual value the promise will return');
  }, 300);
});

// CONTINUATION
anotherPromise
.then(value => { console.log(value) })
Yawning Yacare

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

Синтаксис обещаний в JS


// make new promise ... so this object will promise us that it will hold an Asynchronous action
// some times promise resolve and some times promise get rejected
const A = new Promise((resolve, reject) => {
    setTimeout(() => {
        // here we pass argument if promise get resolved
        resolve('Done');

        // here we pass argument if promise get rejected
        reject('Didn"t');
    }, 3000);
});


// design handling callback function for resolve 
let handleResolvedA = (massage)=>{console.log(massage)}

// design handling callback function for reject 
let handleRejectedA = (massage)=>{console.log(massage)}


// do handling for both
A.then(handleResolvedA, handleRejectedA)
Mr. coder

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

JS обещательный пример

function examplePromise(){
  let promiseToReturn = new Promise(function (resolve, reject) {
    let sum;
    setTimeout(function(){
      sum = 5 + 6;
      if(sum > 10) {
        resolve(sum);
      }else{
        reject('The promise has been rejected');
      }     
    }, 2000);
  });
  return promiseToReturn;
}

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

Ответы похожие на “JS обещательный пример”

Вопросы похожие на “JS обещательный пример”

Больше похожих ответов на “JS обещательный пример” по JavaScript

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

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