“promet un chaînage” Réponses codées

javascript enchaînant la promesse avec alors ()

// returns a promise

let countValue = new Promise(function (resolve, reject) {
  resolve("Promise resolved");
});

// executes when promise is resolved successfully

countValue
  .then(function successValue(result) {
    console.log(result);
  })

  .then(function successValue1() {
    console.log("You can call multiple functions this way.");
  });
SAMER SAEID

promet un chaînage

/* Let's break n STEP-BY-STEP (cpy paste n console u'll have clear view)

1. Promises chaining will allow us to add many async operations
 in a sequence way (i.e  in order)
2. But like we have callback hell, here our code at the end 
will be more difficult to read
3. TIPS : Best options are go with >> {FETCH API, Async/Await} 
4. NOTE : Learn promises once, understand no.3 in a easy way, above
both completely relay on Promises concept, makesure u understand them
clearly. */


//EXAMPLE

var result = true;
var learsJS = new Promise((resolve,reject) => {
  setTimeout(() => {
    if(result){ //if true resolve below case
    resolve("This is my 1st resolved case");
  }
    else{
    reject("I need some more time");
  }
  }, 2000);
}).then(resolvedData1 => {
	console.log(resolvedData1); //resolved promise1 - 5th line
  return new Promise((resolve) => { // returning another promise2
    resolve(`${resolvedData1} -- This is 2nd resolved case`)
  });
})
.then(resolvedData2 => {
  console.log(resolvedData2); //resolved promise2
	return new Promise((resolve) => { // returning another promise3
    resolve(` ${resolvedData2} -- This is my 3rd resolved case`)
  })
})
.then(resolvedData3 => {
  console.log(resolvedData3); //resolved promise3
	return new Promise((resolve) => { //returning final promise
    resolve(` ${resolvedData3} -- This is my Final resolved case`)
  })
})
.then(finalData => console.log(finalData)); //resolved final promise
Outlander

Réponses similaires à “promet un chaînage”

Questions similaires à “promet un chaînage”

Plus de réponses similaires à “promet un chaînage” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code