ECMAScript 2017 Preview
Array.prototype.includes
Array.prototype.includes(value[, index]): boolean |
Why includes
, we have indexOf
already?
> [NaN].indexOf(NaN); |
Exponentiation operator
> let squared = 3 ** 2; |
Trailing commas in function parameter lists and calls
> function foo( |
String.prototype.padStart & String.prototype.padEnd
String.prototype.padStart(maxLength, fillString=’ ‘)
> 'x'.padStart(5, 'ab'); |
Object.values & Object.entries
> let obj = {foo: "bar", baz: 7} |
Object.getOwnPropertyDescriptors
> let obj= {}; |
Use cases
copying properties into an object
Since ES6, JavaScript already has a tool method for copying properties: Object.assign(). However, this method uses simple get and set operations to copy a property whose key is key. That means that it doesn’t properly copy properties with non-default attributes (getters, setters, non-writable properties, etc.).
> const source = { |
cloning objects
> const clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj)); |
Async functions
Overview
Variants
> async function foo() {} |
Always return Promises
> async function foo() { |
Handling results and errors of asynchronous computations via await
> async function foo() { |
Understanding async functions
// Promise |
Async functions are started synchronously, settled asynchronously
> async function asyncFunc() { |
Tips for Async functions
Don’t forget await
> async function asyncFunc() { |
You don’t need await if you “fire and forget”
> async function asyncFunc() { |
await is sequential, Promise.all() is parallel
> async function foo() { |
Async functions and callbacks
Array.prototype.map
> async function downloadContent(urls) { |
Array.prototype.forEach
> async function logContent(urls) { |