Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript Chaining the Promise with then()

// 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.");
  });
Comment

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: angularjs ng-options name value 
Javascript :: Pass string using a function 
Javascript :: logical operators in javascript 
Javascript :: cypress set date to specific date 
Javascript :: spread and rest operator javascript 
Javascript :: JavaScript if, else, and else if 
Javascript :: passing json as datasource to jasper report library 
Javascript :: send data using axios 
Javascript :: how to append a data to list in redux 
Javascript :: production server next.js 
Javascript :: javascript save as pdf 
Javascript :: prototype javascript 
Javascript :: react validation form 
Javascript :: js dataset 
Javascript :: d-block d-none js 
Javascript :: use the AJAX XMLHttpRequest object in Javascript to send json data to the server 
Javascript :: inline style to change background color js 
Javascript :: library for react table 
Javascript :: the filter array 
Javascript :: Truncate a string using javascript 
Javascript :: javascript Symbol Methods 
Javascript :: javascript constructor 
Javascript :: gps nodejs 
Javascript :: how to use if else statement in javascript 
Javascript :: JavaScript substring Syntax 
Javascript :: sort array descending 
Javascript :: $(...).DataTable is not a function 
Javascript :: CHECKING TYPE OF ARRAY 
Javascript :: redux action creators 
Javascript :: javascript constants 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =