Skip to content

Instantly share code, notes, and snippets.

@sprakash57
Last active October 26, 2020 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sprakash57/124ee72f2c8a3a38b7bf65ba68918860 to your computer and use it in GitHub Desktop.
Save sprakash57/124ee72f2c8a3a38b7bf65ba68918860 to your computer and use it in GitHub Desktop.
ES2020 summary
/* 1. BigInt */
conosle.log(typeof 22n); // bigint
console.log(25n > 20); // true
console.log("1" + 10n); // "11"
console.log(34n.toString()); // "34"
console.log(2n == 2) //true
console.log(2n === 2) //false
/* 2. Promise.allSettled() */
// Output will be array of resolved and rejected objects
Promise.allSettled([
new Promise(res => setTimeout(() => res(1), 3000)),
new Promise((res, rej) => setTimeout(() => rej(new Error("Oops")), 5000)),
new Promise(res => setTimeout(() => resolve(3), 1000))
]).then(data => console.log(data));
Output -
[
{ status: 'fulfilled', value: 1 },
{
status: 'rejected',
reason: Error: oops
.....
.....
},
{ status: 'fulfilled', value: 3 }
]
// All resolved, otherwise only rejected promise.
Promise.all([
new Promise(res => setTimeout(() => res(1), 3000)),
new Promise((res, rej) => setTimeout(() => rej(new Error("Oops")), 5000)),
new Promise(res => setTimeout(() => resolve(3), 1000))
]).then(data => console.log(data));
/* 3. Nullish Coalescing (??) */
// Strictly checks for undefined.
console.log(undefined || "1"); // "1"
console.log(undefined ?? "1"); // "1"
console.log(0 || "1"); // "1"
console.log(0 ?? "1"); // 0
/* 4. Optional chaining (?) */
// Access deeply nested object properties without worrying about reference error.
const response = {
first: {
second: {
third: "this you want to access"
}
}
}
console.log(first.otherSecond.third); //Reference error
console.log(first?.otherSecond?.third); //undefined
/* 5. globalThis */
globalThis.log = "This will be accessible everywhere".
/* 6. Dynamic import */
//print.js
const print = (value) => `Hi ${value}`
export { print };
//greet.js
const greet = value => import('./print.js).then(func => func(value));
greet("sunny"); //Hi sunny
/* 7.String.prototype.matchAll() */
// Replacement of regex.exec(). Returns iterator.
const regexp = RegExp('[a-z]*ame','g');
const str = 'rame suot name vjaoi game';
let match;
while ((match = regexp.exec(str)) !== null) {
console.log(match)
}
// Same as
console.log(...str.matchAll(regexp));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment