New Features - ChoDragon9/posts GitHub Wiki

Data Structure

Map

  • Any value can be a key, even an object
  • Getting an unknown key produces undefined
const map = new Map()
map.set('foo', true)
map.set('bar', false)
map.get('foo') //true
map.has('foo') //true
map.delete('foo')
map.size //2
map.clear() //map.size === 0

const map = new Map([  ['foo', true],  ['bar', false]])

WeakMap

const weakMap = new WeakMap()
let obj = {}
weakMap.set(obj, false)
console.log(weakMap.get(obj)) //false
obj = null
// obj in weakMap is garbage-collected

//only get, set, has, delete methods

Set

const set = new Set()
set.add('red')
set.has('red') //true
set.delete('red')
set.has('red') //false
set.add('red')
set.add('green')
set.size //2
set.clear() //set.size === 0
const set = new Set(['red', 'green', 'blue'])

//Chainable
set.add('purple').add('black')

WeakSet

const weakSet = new WeakSet()
let obj = {}
weakSet.add(obj)
weakSet.has(obj) //true
obj = null
// obj in weakSet is garbage-collected

//only add, has, delete methods

Promise

Promise๋Š” ๋Œ€๊ธฐ, ์ดํ–‰, ๊ฑฐ๋ถ€ ์ƒํƒœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋Œ€๊ธฐ : ์ดˆ๊ธฐ์ƒํƒœ
  • ์ดํ–‰ : ์„ฑ๊ณต ์ƒํƒœ, resolve(), Promise.resolve()
  • ๊ฑฐ๋ถ€ : ์‹คํŒจ ์ƒํƒœ, reject(), Promise.reject()

Usage

resolve/reject

const promise = new Promise((resolve, reject) => {
  getData(
    response => resolve(response.data), 
    error => reject(error.message)
  )
})

then / catch

promise
  .then(data => console.log(data))
  .catch(err => console.error(err))

Multiple

all

Promise.all([
  getPromise(),
  getPromise(),
  getPromise()
])  //response all data
  .then(data => console.log(data))
  .catch(err => console.error(err))

race

Promise.race([
  getPromise(), //1000ms
  getPromise(), //500ms
  getPromise() //250ms
])  //response of 250ms
  .then(data => console.log(data))
  .catch(err => console.error(err))