es6: update with async-await and spread

This commit is contained in:
Rico Sta. Cruz 2017-10-21 03:44:02 +08:00
parent ff8f02c2cb
commit d1ac289c31
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 68 additions and 1 deletions

69
es6.md
View File

@ -3,7 +3,7 @@ title: ES2015+
category: JavaScript category: JavaScript
layout: 2017/sheet layout: 2017/sheet
tags: [Featured] tags: [Featured]
updated: 2017-10-02 updated: 2017-10-21
weight: -10 weight: -10
intro: | intro: |
A quick overview of new JavaScript features in ES2015, ES2016, ES2017 and beyond. A quick overview of new JavaScript features in ES2015, ES2016, ES2017 and beyond.
@ -162,6 +162,21 @@ Promise.reject(···)
Promise.resolve(···) Promise.resolve(···)
``` ```
### Async-await
```js
async function run () {
const user = await getUsers()
const tweets = await getTweets(user)
return [user, tweets]
}
```
{: data-line="2,3"}
`async` functions are another way of using functions.
See: [async function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
Destructuring Destructuring
------------- -------------
{: .-three-column} {: .-three-column}
@ -212,6 +227,58 @@ for (let {title, artist} in songs) {
The assignment expressions work in loops, loo. The assignment expressions work in loops, loo.
Spread
------
### Object spread
#### with Object spread
```js
const options = {
...defaults,
visible: true
}
```
{: data-line="2"}
#### without Object spread
```js
const options = Object.assign(
{}, defaults,
{ visible: true })
```
The Object spread operator lets you build new objects from other objects.
See: [Object spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator)
### Array spread
#### with Object spread
```js
const users = [
...admins,
...editors,
'rstacruz'
]
```
{: data-line="2,3"}
#### without Object spread
```js
const users = admins
.concat(editors)
.concat([ '@rstacruz' ])
```
The spread operator lets you build new arrays in the same way.
See: [Spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator)
Functions Functions
--------- ---------