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

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

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

Programa JavaScript con una promesa

const count = true;

let countValue = new Promise(function (resolve, reject) {
    if (count) {
        resolve("There is a count value.");
    } else {
        reject("There is no count value");
    }
});

console.log(countValue);
SAMER SAEID

Promesa en JavaScript

/*
A promise is a building object of JavaScript, using it we can easily do 
asynchronous tasks. Also, the concept that is used to create clean code 
basically promises.
*/
//Promise
let firstPromise = new Promise((resolved, reject) => {
    let fullName = 'Muhammad Shahnewaz';
    setTimeout(() => resolved(fullName), 3000);  //we need to use setTimeout() 
}).then((name) => {
    console.log('I am ' + name);    //Muhammad Shahnewaz
});
Tiny Coders

Respuestas similares a “Promesa en JavaScript”

Preguntas similares a “Promesa en JavaScript”

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

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código