πŸ€ κΈ°μˆ μ±… μŠ€ν„°λ””

23λ…„ 1μ›”λΆ€ν„° ν™œλ™ 쀑인 κ΅μœ‘μ—μ„œ, 뜻이 λ§žλŠ” λ™λ£Œλ“€κ³Ό ν•¨κ»˜ μ§„ν–‰ν•˜κ²Œ 된 μŠ€ν„°λ””
처음 μ½”μ–΄ μžλ°”μŠ€ν¬λ¦½νŠΈ μ„œμ μ„ 읽자고 μ œμ•ˆν•˜λ©΄μ„œ μ‹œμž‘!
μ•žμœΌλ‘œλ„ κΎΈμ€€νžˆ 기술 μ„œμ μ„ 읽고 ν•¨κ»˜ λ°œμ „ν•˜λŠ” μ‹œκ°„μ΄ λ˜μ—ˆμœΌλ©΄ μ’‹κ² λ‹€!

책에 μžˆλŠ” λ‚΄μš©λ³΄λ‹¨, μ½μœΌλ©΄μ„œ 혼자 μ •λ¦¬ν•œ λ‚΄μš©λ“€ μœ„μ£Όλ‘œ μž‘μ„±ν•˜μž!

πŸ… μ½”μ–΄ μžλ°”μŠ€ν¬λ¦½νŠΈ 4. μ½œλ°±ν•¨μˆ˜

μΌμ‹œ: 2μ›” 19일 22μ‹œ


Quiz1)p101. 예제 4-5. / μ°Έκ³ ν•˜μ—¬ filter λ©”μ„œλ“œ κ΅¬ν˜„ν•΄λ³΄κΈ°

// 예제 4-5
Array.prototype.map2 = function(callback, thisArg) {
  var mappedArr = [];
  for (var i = 0; i < this.length; i++) {
    var mappedValue = callback.call(thisArg || window, this[i], i, this);
    mappedArr[i] = mapppedValue;
  }
  return mappedArr;
}

μ˜ˆμƒ λ‹΅μ•ˆ

Array.prototype.filter2 = function(callback, thisArg) {
  var filteredArr = [];
  for (var i = 0; i < this.length; i++) {
    var isTrue = callback.call(thisArg || window, this[i], i, this);
    if (isTrue) {
      filteredArr.push(this[i]);
    }
  }
  return filteredArr;
}

Quiz2)p111. 예제4-15. / 12~15번째 쀄 콜백 ν•¨μˆ˜ ν‘œν˜„ λ°”κΎΈκΈ°

var addCoffee = function(name) {
  return function(prevName) {
    return new Promise(function(resolve) {
      setTimeout(function() {
        var newName = prevName ? (prevName + ', ' + name) : name;
        console.log(newName);
        resolve(newName);
      }, 500);
    });
  };
};
// 이 μ•„λž˜ μ½”λ“œλ“€
addCoffee('μ—μŠ€ν”„λ ˆμ†Œ')()
.then(addCoffee('아메리카노'))
.then(addCoffee('카페λͺ¨μΉ΄'))
.then(addCoffee('μΉ΄νŽ˜λΌλ–Ό'))

μ˜ˆμƒ λ‹΅μ•ˆ

addCoffee('μ—μŠ€ν”„λ ˆμ†Œ')()
.then(prevName => addCoffee('아메리카노')(prevName))
.then(prevName => addCoffee('카페λͺ¨μΉ΄')(prevName))
.then(prevName => addCoffee('μΉ΄νŽ˜λΌλ–Ό')(prevName))

// λΉ„μŠ·ν•œ μ˜ˆμ‹œ
[1, 2, 3, 4, 5].forEach(console.log) 

p113. async/await μ‰¬μš΄ 예제

async function getDelay(target, second) {
	return new Promise((resolve, reject) => {
		setTimeout(() => resolve(target), second * 1000);
    })
	}

console.log(await getDelay('비동기', 5)); // 5초 뒀에 '비동기' 좜λ ₯