“Promesa de objetos JavaScript” 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

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 de objetos 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"
Lonely Lyrebird

Promesa 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

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

Promesa 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

Respuestas similares a “Promesa de objetos JavaScript”

Preguntas similares a “Promesa de objetos JavaScript”

Más respuestas relacionadas con “Promesa de objetos JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código