티스토리 뷰

공부일지(TIL)/JavaScript

ES9 features

Alledy 2019. 4. 16. 00:39

ES9 features

  • Object spread operator
const animals = {
  tiger: 23,
  lion: 5,
  monkey: 2
}

const {tiger, ...rest} = animals;

tiger // prints 23 
rest // prints {lion: 5, monkey: 2}

 

  • Finally
const urls = [
  'https://jsonplaceholder.typicode.com/users',
  'https://jsonplaceholder.typicode.com/posts',
  'https://jsonplaceholder.typicode.com/albums'
]

Promise.all(urls.map(url => {
  return fetch(url).then(resp => resp.json())
})).then(result => {
  console.log(result[0])
  console.log(result[1])
  console.log(result[2]) 
})
.catch(err => console.log('uhhhhh fix it!', err))
.finally(() => console.log('extra'));

finally는 promise가 다 끝나고 나서 실행되는 코드이다. 프로미스가 에러가 나든 성공하든 상관없이 실행된다.

 

  • for await of
const getData2= async function() {
  const arrayOfPromises = urls.map(url => fetch(url));
  for await (let request of arrayOfPromises) {
    const data = await request.json()
    console.log(data)
  }
}

기존 array의 for of구문이 async await에 적용된 것이다. map을 사용해서 처리해도 되지만 for of를 통해서 promise를 iterate할 수 있다는 것을 보여주기 위해서 사용했다.

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함