“Definisi janji dalam JavaScript” Kode Jawaban

JavaScript menjanjikan

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

Janji 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"
Unusual Unicorn

Janji JavaScript

let num = 10;
//call back function
const promis = new Promise(function (resolve, reject) {

  if (num > 5) {

    //this resolve method will send data to resoleveData variable
    resolve(" Problem resolved successfully")
  }
  else {
    //this reject method will send data to rejectData variable
    reject("sorry problem couldn't solve")
  }

})
//resoleveData variable
promis.then(function (resolveData) {

  console.log(resolveData)
  //rejectData variable
}).catch(function (rejectData) {

  console.log(rejectData)
})
SkiLL3r

Apa janji dalam javascript

Promises are used to handle asynchronous operations in JavaScript.
They are easy to manage when dealing with multiple asynchronous operations
where callbacks can create callback hell leading to unmanageable code.
Raviraj Solanki

JavaScript menjanjikan

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

Definisi janji dalam JavaScript

A promise is an object that may produce a single value sometime in the future: either a resolved value, or a reason that it's not resolved (e.g., a network error occurred)
Annoyed Aardvark

Jawaban yang mirip dengan “Definisi janji dalam JavaScript”

Pertanyaan yang mirip dengan “Definisi janji dalam JavaScript”

Lebih banyak jawaban terkait untuk “Definisi janji dalam JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya