ES2021 Features

ReplaceAll string

				
					//Replace all Anurag with Abhishek
let string = "Anurag is Anurag";
console.log(string.replace(/Anurag/g, "Abhishek"));
//output:- Abhishek is Abhishek
				
			
				
					//Replace all Anurag with Abhishek
let string = "Anurag is Anurag";
console.log(string.replaceAll("Anurag", "Abhishek"));
//output:- Abhishek is Abhishek


				
			

Promise.Any()

				
					//Race

/*
1.The first one reaches is p1
!p1 reaches immediately - Rejected
!p2 will take 6 seconds - Resolved
!p3 will take 5 seconds - Resolved 
!p4 will take 5 seconds - Rejected

!!p1 wins 
It's a race ..The one who reaches first decides
a) if the first reaches is resolved it will go to resolved then block
b) if the first one reaches is errored --> goes to catch block
p1 is failed -goes to catch block
output==> error::-> p1 failed immediately

*/

Promise.race([p1, p2, p3, p4])
  .then((value) => {
    console.log("resolved race::->", value);
  })
  .catch((error) => {
    console.log("error race::->", error);
  });

//All
/*
if all are resolved --> goes to then block
if any one fails --> goes to catch block
!Here all are not resolved, p1 fails , p4 fails
output==> error All::-> p1 failed immediately
*/

Promise.all([p1, p2, p3, p4])
  .then((value) => {
    console.log("resolved All::->", value);
  })
  .catch((error) => {
    console.log("error All::->", error);
  });

//Any
/*
If any one succeeds --> goes to then block
out of p1,p2,p3 and p4 --> p3 is resolved first 
output==> resolved Any::-> p3 success after 5 seconds
*/

Promise.any([p1, p2, p3, p4])
  .then((value) => {
    console.log("resolved Any::->", value);
  })
  .catch((error) => {
    console.log("error Any::->", error);
  });

//AllSettled
//Irrespective of any promises resolves or rejects
//It never goes to catch block
Promise.allSettled([p1, p2, p3, p4])
  .then((value) => {
    console.log("resolved allSettled::->", value);
  })
  .catch((error) => {
    console.log("error allSettled::->", error);
  });

				
			
				
					
//Any
/*
If any one succeeds --> goes to then block
out of p1,p2,p3 and p4 --> p3 is resolved first 
output==> resolved Any::-> p3 success after 5 seconds
*/

Promise.any([p1, p2, p3, p4])
  .then((value) => {
    console.log("resolved Any::->", value);
  })
  .catch((error) => {
    console.log("error Any::->", error);
  });



				
			

Numerical Separator

				
					let oneCrore5lakh20k573 = 10520573;
console.log(oneCrore5lakh20k573);
				
			
				
					let oneCrore5lakh20k573 = 1_05_20_573;
console.log(oneCrore5lakh20k573);//10520573
				
			

Leave a Comment