“Ejemplo de JS Promise” Código de respuesta

Promesas de 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 Promise Ejemplo básico

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

Promesa de 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

Promesa sintaxis en 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

Promesas de 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

Ejemplo de JS Promise

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

Respuestas similares a “Ejemplo de JS Promise”

Preguntas similares a “Ejemplo de JS Promise”

Más respuestas relacionadas con “Ejemplo de JS Promise” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código