Answers for "promises chaining"

2

javascript promise chain

// base case 
promise
  .then(...)
  .then(...)
  .then(...)
  .catch(...)

//real world
coinflip(10)
  .then(betAgain)
  .then(betAgain)
  .then(betAgain)
  .then(result => {
    console.log(`OMG, WE DID THIS! TIME TO TAKE ${result} HOME!`);
  })
  .catch(handleRejection);
Posted by: Guest on February-16-2021
3

promise chaining

new Promise(function(resolve, reject) {

  setTimeout(() => resolve(1), 1000); // (*)

}).then(function(result) { // (**)

  alert(result); // 1
  return result * 2;

}).then(function(result) { // (***)

  alert(result); // 2
  return result * 2;

}).then(function(result) {

  alert(result); // 4
  return result * 2;

});
Posted by: Guest on June-21-2020
0

promises chaining

/* 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
Posted by: Guest on August-15-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language