Skip to content

Latest commit

 

History

History
63 lines (49 loc) · 1.73 KB

File metadata and controls

63 lines (49 loc) · 1.73 KB

ES9 new features

  • Apply spread and rest statements to objects.
  • Asynchronous Iterators.
  • Method Promise.prototype.finally().
  • Regular expression enhancements.

  • Application of spread and rest operators now to objects (before only arrays) // spread & rest operator on Array
const numbers = [1, 2, 3, 4, 5];
const sum = (a, b, c, d, e) => a + b + c + d + e;
const res = sum(...numbers);
console.log(res) //15
// spread & rest operator on Object
let { x, ...remaining } = { x: 1, a: 2, b: 3, c: 4 };
console.info(x); // 1
console.info(remaining); // {a: 2, b: 3, c: 4}
// spread & rest operator on Object
let existingObject = { x: 1, a: 2, b: 3, c: 4 };
let newObject = { ...existingObject, foo: () => { } };
console.info(newObject); // {x: 1, a: 2, b: 3, c: 4, foo: ƒ}
  • Asynchronous iterators The new design for-await-ofallows you to call asynchronous functions that return promises in cycles. Such loops await the resolution of the promise before moving on to the next step. Here is how it looks.
for await (const line of readLines(filePath)) {
  console.log(line)
}
  • Promise.prototype.finally() method If the promise is successfully resolved, the next method is called then(). If something goes wrong, the method is called catch(). T he method finally()allows you to execute some code regardless of what happened before.
// fetch json file triggering a Promise()
fetch('file.json')
  .then(data => data.json())
  .catch(error => console.error(error))
  .finally(() => console.log('finished'))
  • Regular Expression Improvements
console.log(/hi.welcome/.test('hi\nwelcome')) // false
console.log(/hi.welcome/s.test('hi\nwelcome')) // true