์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋…ธ๋“œ 2ํŒ€] #3.์•Œ์•„๋‘ฌ์•ผ ํ•  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

24-25/Node.js 2

by sksmsyena 2024. 10. 11. 12:22

๋ณธ๋ฌธ

728x90

 
๐ŸŒŸํ‚ค์›Œ๋“œ: ES2015+๋ฌธ๋ฒ•, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜, ํ”„๋กœ๋ฏธ์Šค(Promise), async/await, ๋„๋ณ‘ํ•ฉ/์˜ต์…”๋„ ์ฒด์ด๋‹
 


1. ES2015+ ๋ฌธ๋ฒ•

- 2015๋…„์„ ๊ธฐ์ ์œผ๋กœ ๋งค๋…„ ๋ฌธ๋ฒ• ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ๋ฐœํ‘œ๋˜์—ˆ๋‹ค.
- ES2015 ์ด์ƒ์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ‹€์–ด ES2015+๋ผ ํ‘œํ˜„ํ•œ๋‹ค.
- ์‹ค๋ฌด์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„ ์ตœ์‹  ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•๋ถ€ํ„ฐ ์ตํž ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
 
1-1. const, let
- var ๋ณ€์ˆ˜๋Š” const์™€ let์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค

if (true) {
  var x = 3;
}
console.log(x); // 3

if (true) {
  const y = 3;
}
console.log(y); // Uncaught ReferenceError: y is not defined

 
โ‘  var์™€ const, let์˜ ์ฐจ์ด
var: ํ•จ์ˆ˜ ์Šค์ฝ”ํ”„๋ฅผ ๊ฐ€์ง€๋ฉฐ, if๋ฌธ์˜ ๋ธ”๋ก๊ณผ ๊ด€๊ณ„์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
const, let: ๋ธ”๋ก ์Šค์ฝ”ํ”„๋ฅผ ๊ฐ€์ง€๋ฉฐ, ๋ธ”๋ก ๋ฐ–์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค.
โˆด ํ•จ์ˆ˜ ์Šค์ฝ”ํ”„ ๋Œ€์‹  ๋ธ”๋ก ์Šค์ฝ”ํ”„๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ํ˜ธ์ด์ŠคํŒ…๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๋„ ํ•ด๊ฒฐํ•˜๊ณ , ์ฝ”๋“œ ๊ด€๋ฆฌ๋„ ์ˆ˜์›”ํ•˜๋‹ค.
 
โ‘ก const์™€ let์˜ ์ฐจ์ด
const(์ƒ์ˆ˜): ๊ฐ’์„ ํ•œ ๋ฒˆ ํ• ๋‹นํ•˜๋ฉด ๋‹ค๋ฅธ ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค, ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ๊ฐ’์„ ํ• ๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

const a = 0;
a = 1; // Uncaught TypeError: Assignment to constant variable

let b = 0;
b = 1; // 1

const c; // Uncaught SyntaxError: Missing initializer in const declaration

 
1-2. ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด
- ํฐ๋”ฐ์˜ดํ‘œ, ์ž‘์€๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ๋Š” ๊ธฐ์กด ๋ฌธ์ž์—ด๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋ฐฑํ‹ฑ(`)์œผ๋กœ ๊ฐ์Œ€ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฌธ์ž์—ด ์•ˆ์— ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
- ${๋ณ€์ˆ˜}ํ˜•์‹์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ + ๊ธฐํ˜ธ ์—†์ด ๋ฌธ์ž์—ด์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

const num3 = 1;
const num4 = 2;
const result2 = 3;
const string2 = `${num3} ๋”ํ•˜๊ธฐ ${num4}๋Š” ' ${result2}'`;
console.log(string2); // 1 ๋”ํ•˜๊ธฐ 2๋Š” '3'

 
1-3. ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด

var sayNode = function() {
  console.log('Node');
};
var es = 'ES';
var oldObject = {
  sayJS: function() {
    console.log('JS');
  },
  sayNode: sayNode,
};
oldObject[es + 6] = 'Fantastic';
oldObject.sayNode(); // Node
oldObject.sayJS(); // JS
console.log(oldObject.ES6); // Fantastic

 

const newObject = {
  sayJS() {
    console.log('JS');
  },
  sayNode,
  [es + 6]: 'Fantastic',
};
newObject.sayNode(); // Node
newObject.sayJS(); // JS
console.log(newObject.ES6); // Fantastic

- ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ์— ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐํ•  ๋•Œ ์ฝœ๋ก (:)๊ณผ function์„ ๋ถ™์ด์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
- ์†์„ฑ๋ช…๊ณผ ๋ณ€์ˆ˜๋ช…์ด ๋™์ผํ•œ ๊ฒฝ์šฐ ํ•œ ๋ฒˆ๋งŒ ์จ๋„ ๋œ๋‹ค. (์ฝ”๋“œ์˜ ์ค‘๋ณต์„ ํ”ผํ•  ์ˆ˜ ์žˆ์–ด ํŽธ๋ฆฌํ•˜๋‹ค.)
- ES6๋ผ๋Š” ์†์„ฑ๋ช…์„ ์œ„ํ•ด ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด(oldObject) ๋ฐ–์—์„œ [es+6]์„ ํ•  ํ•„์š” ์—†์ด, newObject์•ˆ์— [es+6]์ด ์†์„ฑ๋ช…์œผ๋กœ ๋ฐ”๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ์•ˆ์— ๋™์  ์†์„ฑ ์„ ์–ธ ๊ฐ€๋Šฅํ•˜๋‹ค.
 
โˆด  ์ต์ˆ™ํ•ด์ง€๋ฉด ์ฝ”๋“œ์˜ ์–‘์„ ๋งŽ์ด ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
 
1-4. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜(arrow function)
โ‘  ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ์ ์ธ ํ˜•ํƒœ

function add1(x, y) {
  return x + y;
}

const add2 = (x, y) => {
  return x + y;
};

const add3 = (x, y) => x + y;

const add4 = (x, y) => (x + y);

function not1(x) {
  return !x;
}

const not2 = x => !x;

- function ์„ ์–ธ ๋Œ€์‹  => ๊ธฐํ˜ธ๋กœ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.
- ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๋ฉด ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
- ๋‚ด๋ถ€์— return๋ฌธ ๋ฐ–์— ์—†๋Š” ๊ฒฝ์šฐ, return๋ฌธ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. (์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.)
 
โ‘ก this ๋ฐ”์ธ๋“œ ๋ฐฉ์‹์˜ ๋ณ€ํ™”

var relationship1 = {
  name: 'zero',
  friends: ['nero', 'hero', 'xero'],
  logFriends: function () {
    var that = this; // relationship1์„ ๊ฐ€๋ฆฌํ‚ค๋Š” this๋ฅผ that์— ์ €์žฅ
    this.friends.forEach(function (friend) {
      console.log(that.name, friend);
    });
  },
};
relationship1.logFriends();

const relationship2 = {
  name: 'zero',
  friends: ['nero', 'hero', 'xero'],
  logFriends() {
    this.friends.forEach(friend => {
      console.log(this.name, friend);
    });
  },
};
relationship2.logFriends();

- relationship1: ๊ฐ์ž ๋‹ค๋ฅธ ํ•จ์ˆ˜ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ that์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด relationship1์— ๊ฐ„์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•œ๋‹ค.
- relationship2: ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฏ€๋กœ์จ ๋ฐ”๊นฅ ์Šค์ฝ”ํ”„์ธ logFriends()์˜ this๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
 
1-5. ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น

var candyMachine = {
  status: {
    name: 'node',
    count: 5,
  },
  getCandy: function () {
    this.status.count--;
    return this.status.count;
  },
};
var getCandy = candyMachine.getCandy;
var count = candyMachine.status.count;

 

const candyMachine = {
  status: {
    name: 'node',
    count: 5,
  },
  getCandy() {
    this.status.count--;
    return this.status.count;
  },
};
const { getCandy, status: { count } } = candyMachine;

๊ฐ์ฒด ์•ˆ์˜ ์†์„ฑ์„ ์ฐพ์•„ ๋ณ€์ˆ˜์™€ ๋งค์นญํ•˜๋ฉฐ, count์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„ ์•ˆ์˜ ์†์„ฑ๋„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
 
cf) ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๋ฌธ๋ฒ•

const array = [โ€˜nodejsโ€™, {}, 10, true];
const [node, obj, , bool] = array;

-  ๋…ธ๋“œ๋Š” ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ์ด์šฉํ•˜๋ฏ€๋กœ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
 
1-6. ํด๋ž˜์Šค
- ํด๋ž˜์Šค ๋ฌธ๋ฒ•์ด ์ถ”๊ฐ€๋˜์—ˆ์ง€๋งŒ, ํด๋ž˜์Šค ๊ธฐ๋ฐ˜์ด ์•„๋‹Œ ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
- ๋ณด๊ธฐ ์ข‹๊ฒŒ ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜ ๋ฌธ๋ฒ•์„ ํด๋ž˜์Šค๋กœ ๋ฐ”๊พผ ๊ฒƒ์ด๋‹ค.

class Human {
  constructor(type = 'human') {
    this.type = type;
  }

  static isHuman(human) {
    return human instanceof Human;
  }

  breathe() {
    alert('h-a-a-a-m');
  }
}

class Zero extends Human {
  constructor(type, firstName, lastName) {
    super(type);
    this.firstName = firstName;
    this.lastName = lastName;
  }

  sayName() {
    super.breathe();
    alert(`${this.firstName} ${this.lastName}`);
  }
}

const newZero = new Zero('human', 'Zero', 'Cho');
Human.isHuman(newZero); // true

 
1-7. ํ”„๋กœ๋ฏธ์Šค(Promise)
- ES2015๋ถ€ํ„ฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ๋…ธ๋“œ์˜ API๋“ค์ด ์ฝœ๋ฐฑ ๋Œ€์‹  ํ”„๋กœ๋ฏธ์Šค ๊ธฐ๋ฐ˜์œผ๋กœ ์žฌ๊ตฌ์„ฑ๋˜์—ˆ๋‹ค.
- ์ฝœ๋ฐฑ ์ง€์˜ฅ์„ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.
 
โ‘  ๊ธฐ๋ณธ์ ์ธ Promise ํ˜•ํƒœ 

const condition = true; // true์ด๋ฉด resolve, false์ด๋ฉด reject
const promise = new Promise((resolve, reject) => {
  if (condition) {
    resolve('์„ฑ๊ณต');
  } else {
    reject('์‹คํŒจ');
  }
});
// ๋‹ค๋ฅธ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Œ
promise
  .then((message) => {
    console.log(message); // ์„ฑ๊ณต(resolve)ํ•œ ๊ฒฝ์šฐ ์‹คํ–‰
  })
  .catch((error) => {
    console.error(error); // ์‹คํŒจ(reject)ํ•œ ๊ฒฝ์šฐ ์‹คํ–‰
  })
  .finally(() => { // ๋๋‚˜๊ณ  ๋ฌด์กฐ๊ฑด ์‹คํ–‰
    console.log('๋ฌด์กฐ๊ฑด');
});

 
- new Promise๋กœ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ƒ์„ฑํ•œ ํ›„, resolve, reject๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ–๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๋„ฃ๋Š”๋‹ค.
- ๋งŒ๋“ค์–ด์ง„ promise ๋ณ€์ˆ˜์— ๋ถ™์ธ then, catch ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์‹คํ–‰์ด ์™„๋ฃŒ๋œ ํ›„ ๊ฒฐ๊ด๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.
- Promise๋Š” ์‹คํ–‰์€ ๋ฐ”๋กœ ํ•˜๋˜, ๊ฒฐ๊ด๊ฐ’์€ ๋‚˜์ค‘์— ๋ฐ›๋Š” ๊ฐ์ฒด์ด๋‹ค. (new Promise๋Š” ๋ฐ”๋กœ ์‹คํ–‰๋˜๋‚˜, ๊ฒฐ๊ด๊ฐ’์€ then์„ ๋ถ™์˜€์„ ๋•Œ ๋ฐ›๋Š”๋‹ค.)
 
โ‘ก ํ”„๋กœ๋ฏธ์Šค return, ์—ฌ๋Ÿฌ ๊ฐœ์˜ then๊ณผ catch

promise
  .then((message) => {
    return new Promise((resolve, reject) => {
      resolve(message);
    });
  })
  .then((message2) => {
    console.log(message2);
    return new Promise((resolve, reject) => {
      resolve(message2);
    });
  })
  .then((message3) => {
    console.log(message3);
  })

  .catch((error) => {
    console.error(error);
});

- then์ด๋‚˜ catch์—์„œ ๋‹ค์‹œ ๋‹ค๋ฅธ then์ด๋‚˜ catch๋ฅผ ๋ถ™์ผ ์ˆ˜๋„ ์žˆ๋‹ค. (์ด์ „ then์˜ return ๊ฐ’์„ ๋‹ค์Œ then์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง„๋‹ค.)
- ํ”„๋กœ๋ฏธ์Šค๋ฅผ returnํ•œ ๊ฒฝ์šฐ, ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์ˆ˜ํ–‰๋œ ํ›„ ๋‹ค์Œ then์ด๋‚˜ catch๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
then์—์„œ new Promise๋ฅผ return ํ•ด์•ผ ๋‹ค์Œ then์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
 
โ‘ข ์ค‘์ฒฉ๋œ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ”„๋กœ๋ฏธ์Šค๋กœ ๋ณ€๊ฒฝ

function findAndSaveUser(Users) {
  Users.findOne({})
    .then((user) => {
      user.name = 'zero';
      return user.save();
    })
    .then((user) => {
      return Users.findOne({ gender: 'm' });
    })
    .then((user) => {
      // ์ƒ๋žต
    })
    .catch(err => {
      console.error(err);
    });

- then ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.
- ์ฝœ๋ฐฑ์—์„œ ๋งค๋ฒˆ ๋”ฐ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ–ˆ๋˜ ์—๋Ÿฌ๋„ ๋งˆ์ง€๋ง‰ catch์—์„œ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
- ๋ฉ”์„œ๋“œ๊ฐ€ ํ”„๋กœ๋ฏธ์Šค ๋ฐฉ์‹์„ ์ง€์›ํ•  ๋•Œ๋งŒ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ”„๋กœ๋ฏธ์Šค๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.
cf) ์œ„ ์ฝ”๋“œ์—์„œ๋Š” findOne๊ณผ save ๋ฉ”์„œ๋“œ๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ํ”„๋กœ๋ฏธ์Šค ๊ฐ์ฒด๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ธฐ์— ๊ฐ€๋Šฅํ•˜๋‹ค. 
 
โ‘ฃ Promise.all: ํ”„๋กœ๋ฏธ์Šค ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ํ•œ ๋ฒˆ์— ์‹คํ–‰

const promise1 = Promise.resolve('์„ฑ๊ณต1');
const promise2 = Promise.resolve('์„ฑ๊ณต2');
Promise.all([promise1, promise2])
  .then((result) => {
    console.log(result); // ['์„ฑ๊ณต1', '์„ฑ๊ณต2'];
  })
  .catch((error) => {
    console.error(error);
  });

- Promise.resolve: ์ฆ‰์‹œ resolveํ•˜๋Š” ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
- Promise.reject: ์ฆ‰์‹œ rejectํ•˜๋Š” ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
- Promise.all: ๋ชจ๋‘ resolve๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ then์œผ๋กœ ๋„˜์–ด๊ฐ€๋ฉฐ, result ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ๊ฐ์˜ ํ”„๋กœ๋ฏธ์Šค ๊ฒฐ๊ด๊ฐ’์ด ๋ฐฐ์—ด๋กœ ์ €์žฅ๋œ๋‹ค.
- Promise.all: ํ•˜๋‚˜๋ผ๋„ reject๋˜๋ฉด catch๋กœ ๋„˜์–ด๊ฐ€๋ฉฐ, ์–ด๋–ค ํ”„๋กœ๋ฏธ์Šค๊ฐ€ reject๋œ ์ง€๋Š” ์•Œ ์ˆ˜ ์—†๋‹ค. (์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” Promise.allSettled ์‚ฌ์šฉ์ด ํ•„์š”ํ•˜๋‹ค.)
 
โ‘ค Promise.allSettled

const promise1 = Promise.resolve('์„ฑ๊ณต1');
const promise2 = Promise.reject('์‹คํŒจ2');
const promise3 = Promise.resolve('์„ฑ๊ณต3');
Promise.allSettled([promise1, promise2, promise3])
  .then((result) => {
    console.log(result);
/* [
*    { status: 'fulfilled', value: '์„ฑ๊ณต1' },
*    { status: 'rejected', reason: '์‹คํŒจ2' },
*    { status: 'fulfilled', value: '์„ฑ๊ณต3' }
*  ]
*/
  })
  .catch((error) => {
    console.error(error);
  });

- ์–ด๋–ค ํ”„๋กœ๋ฏธ์Šค๊ฐ€ reject๋œ ์ง€ ์•Œ๊ธฐ ์œ„ํ•ด Promise.allsettled๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (status์—์„œ ์•Œ ์ˆ˜ ์žˆ๋‹ค, reason์€ ์‹คํŒจ ์ด์œ )
- Promise.all ๋Œ€์‹  Promise.allSettled๋ฅผ ์‚ฌ์šฉํ•˜๊ธธ ๊ถŒ์žฅํ•œ๋‹ค.
 
cf) Node 16๋ฒ„์ „ ๋ถ€ํ„ฐ๋Š” reject๋œ Promise์— catch๋ฅผ ๋‹ฌ์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ (๋‹ค์Œ ์ฝ”๋“œ ์‹คํ–‰ ๋ถˆ๊ฐ€), ๋ฐ˜๋“œ์‹œ catch ๋ฉ”์„œ๋“œ๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ ๊ถŒ์žฅํ•œ๋‹ค.

1-8. async/await
- ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋ฅผ ํ•œ ๋ฒˆ ๋” ๊น”๋”ํ•˜๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
 
โ‘  async/await๋ฅผ try/catch๋ฌธ๊ณผ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ํ‘œํ˜„ 

const findAndSaveUser = async (Users) => {
  try {
    let user = await Users.findOne({});
    user.name = 'zero';
    user = await user.save();
    user = await Users.findOne({ gender: 'm' });
    // ์ƒ๋žต
  } catch (error) {
    console.error(error);
  }
};

- ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ ๋Œ€์‹  async function์œผ๋กœ ์„ ์–ธํ•œ๋‹ค. (์œ„ ์ฝ”๋“œ๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ํ‘œํ˜„)
- await Users. findeOne({})์ด resolve๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ ๋’ค user ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
- try/catch๋ฌธ์œผ๋กœ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.
 
โ‘ก for๋ฌธ๊ณผ ํ•จ๊ป˜ ์ˆœ์ฐจ์  ์‹คํ–‰ (๋…ธ๋“œ 10 ๋ฒ„์ „๋ถ€ํ„ฐ ์ง€์›ํ•˜๋Š” ES2018 ๋ฌธ๋ฒ•)

const promise1 = Promise.resolve('์„ฑ๊ณต1');
const promise2 = Promise.resolve('์„ฑ๊ณต2');
(async () => {
  for await (promise of [promise1, promise2]) {
    console.log(promise);
  }
})();

- for await of๋ฌธ์„ ์ด์šฉํ•ด ํ”„๋กœ๋ฏธ์Šค ๋ฐฐ์—ด์„ ์ˆœํšŒํ•œ๋‹ค.
- async ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์€ ํ•ญ์ƒ Promise๋กœ ๊ฐ์‹ธ์ง„๋‹ค.
- ์‹คํ–‰ ํ›„ then์„ ๋ถ™์ด๊ฑฐ๋‚˜ ๋˜ ๋‹ค๋ฅธ asyncํ•จ์ˆ˜ ์•ˆ์—์„œ await๋ฅผ ๋ถ™์—ฌ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
 
1-9. map/set
โ‘  map

const m = new Map();

m.set('a', 'b'); // set(ํ‚ค, ๊ฐ’)์œผ๋กœ Map์— ์†์„ฑ ์ถ”๊ฐ€
m.set(3, 'c'); // ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฐ’์„ ํ‚ค๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค
const d = {};
m.set(d, 'e'); // ๊ฐ์ฒด๋„ ๋ฉ๋‹ˆ๋‹ค

m.get(d); // get(ํ‚ค)๋กœ ์†์„ฑ๊ฐ’ ์กฐํšŒ
console.log(m.get(d)); // e

m.size; // size๋กœ ์†์„ฑ ๊ฐœ์ˆ˜ ์กฐํšŒ
console.log(m.size) // 3

for (const [k, v] of m) { // ๋ฐ˜๋ณต๋ฌธ์— ๋ฐ”๋กœ ๋„ฃ์–ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค
  console.log(k, v); // 'a', 'b', 3, 'c', {}, 'e'
} // ์†์„ฑ ๊ฐ„์˜ ์ˆœ์„œ๋„ ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค

m.forEach((v, k) => { // forEach๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค
  console.log(k, v); // ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๋™์ผ
});

m.has(d); // has(ํ‚ค)๋กœ ์†์„ฑ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค
console.log(m.has(d)); // true

m.delete(d); // delete(ํ‚ค)๋กœ ์†์„ฑ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค
m.clear(); // clear()๋กœ ์ „๋ถ€ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค
console.log(m.size); // 0

- map์€ ๊ฐ์ฒด์™€ ๋น„์Šทํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‚˜, ์ผ๋ฐ˜ ๊ฐ์ฒด์™€๋Š” ๋‹ค๋ฅด๋‹ค.
(์˜ˆ: ์†์„ฑ๋“ค ๊ฐ„์˜ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•œ๋‹ค, ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค, ์†์„ฑ๋ช…์œผ๋กœ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค, size ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์†์„ฑ์˜ ์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค.) 
 
โ‘ก set

const s = new Set();
s.add(false); // add(์š”์†Œ)๋กœ Set์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
s.add(1);
s.add('1');
s.add(1); // ์ค‘๋ณต์ด๋ฏ€๋กœ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค
s.add(2);

console.log(s.size); // ์ค‘๋ณต์ด ์ œ๊ฑฐ๋˜์–ด 4

s.has(1); // has(์š”์†Œ)๋กœ ์š”์†Œ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค
console.log(s.has(1)); // true

for (const a of s) {
  console.log(a); // false 1 '1' 2
}

s.forEach((a) => {
  console.log(a); // false 1 '1' 2
})

s.delete(2); // delete(์š”์†Œ)๋กœ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค
s.clear(); // clear()๋กœ ์ „๋ถ€ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค

 
- set์€ ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.
- ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐฐ์—ด ์ž๋ฃŒ๊ตฌ์กฐ์— ์‚ฌ์šฉ๋œ๋‹ค.
- ๊ธฐ์กด ๋ฐฐ์—ด์—์„œ ์ค‘๋ณต ์ œ๊ฑฐ์—๋„ ์ด์šฉ๋œ๋‹ค.
cf) new Set(๋ฐฐ์—ด): ๋ฐฐ์—ด์˜ ์ค‘๋ณต ์ œ๊ฑฐ, Array.from(Set): Set์„ ๋ฐฐ์—ด๋กœ ๋˜๋Œ๋ฆผ
 
1-10. ๋„ ๋ณ‘ํ•ฉ/์˜ต์…”๋„ ์ฒด์ด๋‹
โ‘  ๋„ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž ??

const c = 0;
const d = c ?? 3; // ?? ์—ฐ์‚ฐ์ž๋Š” null๊ณผ undefined์ผ ๋•Œ๋งŒ ๋’ค๋กœ ๋„˜์–ด๊ฐ
console.log(d); // 0;

const e = null;
const f = e ?? 3;
console.log(f); // 3;

const g = undefined;
const h = g ?? 3;
console.log(h); // 3;

- || ์—ฐ์‚ฐ์ž ๋Œ€์šฉ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
- falsy๊ฐ’ ์ค‘ null๊ณผ undefined๋งŒ ๋”ฐ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.
 
โ‘ก ์˜ต์…”๋„ ์ฒด์ด๋‹ ์—ฐ์‚ฐ์ž ?

const a = {}
a.b; // a๊ฐ€ ๊ฐ์ฒด์ด๋ฏ€๋กœ ๋ฌธ์ œ์—†์Œ

const c = null;
try {
  c.d;
} catch (e) {
  console.error(e); // TypeError: Cannot read properties of null (reading 'd')
}
c?.d; // ๋ฌธ์ œ์—†์Œ

try {
  c.f();
} catch (e) {
  console.error(e); // TypeError: Cannot read properties of null (reading 'f')
}
c?.f(); // ๋ฌธ์ œ์—†์Œ

try {
  c[0];
} catch (e) {
  console.error(e); // TypeError: Cannot read properties of null (reading '0')
}
c?.[0]; // ๋ฌธ์ œ์—†์Œ

- null๊ณผ undefined์˜ ์†์„ฑ์„ ์กฐํšŒํ•˜๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ง‰๋Š”๋‹ค.
์ผ๋ฐ˜์ ์ธ ์†์„ฑ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด๋‚˜ ๋ฐฐ์—ด ์š”์†Œ ์ ‘๊ทผ์— ๋Œ€ํ•ด์„œ๋„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
- c?, .d, c?.f(), c?.[0]์˜ ๊ฐ’์€ undefined์ด๋‹ค.
 


2. ํ”„๋ŸฐํŠธ์—”๋“œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

- HTML์—์„œ script ํƒœ๊ทธ ์•ˆ์— ์ž‘์„ฑํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.
 
2-1. AJAX
- ๋น„๋™๊ธฐ์  ์›น ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค.
- ํŽ˜์ด์ง€ ์ „ํ™˜ ์—†์ด ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์›น ์‚ฌ์ดํŠธ์˜ ๋Œ€๋ถ€๋ถ„์€ AJAX ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•œ๋‹ค.
 
โ‘   GET ์š”์ฒญ ์ด์šฉ

axios.get('https://www.zerocho.com/api/get')
  .then((result) => {
    console.log(result);
    console.log(result.data); // {}
  })
  .catch((error) => {
    console.error(error);
});

- axios.get์˜ ๋‚ด๋ถ€์— newPromise๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ, then๊ณผ catch๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅ ํ•˜๋‹ค.
 

(async () => {
  try {
    const result = await axios.get('https://www.zerocho.com/api/get');
    console.log(result);
    console.log(result.data); // {}
  } catch (error) {
    console.error(error);
  }
})();

- ํ”„๋กœ๋ฏธ์Šค์ด๋ฏ€๋กœ async/await ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
 
โ‘ก  POST ์š”์ฒญ ์ด์šฉ 

(async () => {
  try {
    const result = await axios.post('https://www.zerocho.com/api/post/json', {
      name: 'zerocho',
      birth: 1994,
    });
    console.log(result);
    console.log(result.data);
  } catch (error) {
    console.error(error);
  }
})();

- ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
- GET ์š”์ฒญ์ด๋ฉด axios.get, POST์š”์ฒญ์ด๋ฉด axios.post ์‚ฌ์šฉํ•œ๋‹ค.
 
2-2. FormData
- HTML form ํƒœ๊ทธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์  ์ œ์–ดํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.
 
โ‘   FormData ์ƒ์„ฑ์ž๋กœ formData ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.

const formData = new FormData();
//append ๋ฉ”์„œ๋“œ: ํ‚ค-๊ฐ’ ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ, ํ‚ค ํ•˜๋‚˜์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
formData.append('name', 'zerocho'); 
formData.append('item', 'orange');
formData.append('item', 'melon');
//has ๋ฉ”์„œ๋“œ: ์ฃผ์–ด์ง„ ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€
formData.has('item'); // true
formData.has('money'); // false;
//get ๋ฉ”์„œ๋“œ: ์ฃผ์–ด์ง„ ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’ ํ•˜๋‚˜๋ฅผ ๊ฐ€์ ธ์˜ด
formData.get('item');// orange
//getAll ๋ฉ”์„œ๋“œ: ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ๊ฐ’์„ ๊ฐ€์ ธ์˜ด
formData.getAll('item'); // ['orange', 'melon'];
formData.append('test', ['hi', 'zero']);
formData.get('test'); // hi, zero
//delete ๋ฉ”์„œ๋“œ: ํ˜„์žฌ ํ‚ค๋ฅผ ์ œ๊ฑฐ
formData.delete('test');
formData.get('test'); // null
//set ๋ฉ”์„œ๋“œ: ํ˜„์žฌ ํ‚ค๋ฅผ ์ˆ˜์ •
formData.set('item', 'apple');
formData.getAll('item'); // ['apple'];

โ‘ก axios๋กœ ํผ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์— ์ „์†กํ•œ๋‹ค.

(async () => {
  try {
    const formData = new FormData();
    formData.append('name', 'zerocho');
    formData.append('birth', 1994);
    const result = await axios.post('https://www.zerocho.com/api/post/formdata', formData);
    console.log(result);
    console.log(result.data);
  } catch (error) {
    console.error(error);
  }
})();

 
 
2-3. encodeURIComponent, decodeURIComponent
โ‘  encodeURIComponent
- window ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.
- ์ฃผ์†Œ์— ํ•œ๊ธ€์ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ, ์„œ๋ฒ„๊ฐ€ ํ•œ๊ธ€ ์ฃผ์†Œ๋ฅผ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

(async () => {
  try {
    const result = await axios.get(`https://www.zerocho.com/api/search/${encodeURIComponent('๋…ธ๋“œ')}`);
    console.log(result);
    console.log(result.data); // {}
  } catch (error) {
    console.error(error);
  }
})();

- '๋…ธ๋“œ'๋ผ๋Š” ํ•œ๊ธ€ ์ฃผ์†Œ๊ฐ€ ' %EB%85%B8%EB%93%9C'๋กœ ๋ณ€ํ™˜๋œ๋‹ค.
 
โ‘ก decodeURIComponent

decodeURIComponent('%EB%85%B8%EB%93%9C'); // ๋…ธ๋“œ

 
 ๋ฐ›๋Š” ์ชฝ์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ๊ธ€์ด ์›๋ž˜ ์ƒํƒœ๋กœ ๋ณต๊ตฌ๋œ๋‹ค.
 
2-4. ๋ฐ์ดํ„ฐ ์†์„ฑ๊ณผ dataset
- ๋ฐ์ดํ„ฐ ์†์„ฑ: ๋ณด์•ˆ๊ณผ ๊ด€๋ จ ์—†๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ HTML๊ณผ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ์— ์ €์žฅํ•˜๋Š” ๊ณต์‹์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

<ul>
//HTML ํƒœ๊ทธ์˜ ์†์„ฑ์œผ๋กœ data-๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ๋“ค์„ ๋„ฃ์Œ (๋ฐ์ดํ„ฐ ์†์„ฑ)
  <li data-id="1" data-user-job="programmer">Zero</li>
  <li data-id="2" data-user-job="designer">Nero</li>
  <li data-id="3" data-user-job="programmer">Hero</li>
  <li data-id="4" data-user-job="ceo">Kero</li>
</ul>
<script>
  console.log(document.querySelector('li').dataset);
  //dataset ์†์„ฑ์„ ํ†ตํ•ด ์ฒซ ๋ฒˆ์งธ li ํƒœ๊ทธ์˜ ๋ฐ์ดํ„ฐ ์†์„ฑ์— ์ ‘๊ทผ
  // { id: '1', userJob: 'programmer' } (๋ฐ์ดํ„ฐ ์†์„ฑ์˜ ์ด๋ฆ„์ด ์กฐ๊ธˆ์”ฉ ๋ณ€ํ˜•๋จ)
</script>

- ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์‰ฝ๊ฒŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.
cf) ๋ฐ˜๋Œ€๋กœ dataset์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด๋„ HTML ํƒœ๊ทธ์— ๋ฐ˜์˜๋œ๋‹ค.
(์˜ˆ: dataset.monthSalary=10000; ์„ ๋„ฃ์œผ๋ฉด data-month-salary="10000" ์†์„ฑ์ด ์ƒ๊ธด๋‹ค.)


 

quiz
1. ES2015+๋ฌธ๋ฒ•์—์„œ, (  )๋ณ€์ˆ˜๋Š” (  )๋ณ€์ˆ˜์™€ (  )๋ณ€์ˆ˜๋กœ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉํ•œ๋‹ค.
 
2. (  ) ๋ฌธ๋ฒ•์ด ์ถ”๊ฐ€๋˜์—ˆ์ง€๋งŒ, (  )๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
 
3. Promise๋Š” (  )์€ ๋ฐ”๋กœ ํ•˜์ง€๋งŒ, (  )์€ ๋‚˜์ค‘์— ๋ฐ›๋Š” ๊ฐ์ฒด์ด๋‹ค.
 
4. then์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” Promise๋ฌธ์˜ ๊ฒฝ์šฐ, then์—์„œ new Promise๋ฅผ (  )ํ•ด์•ผ ๋‹ค์Œ then์ด ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.
 
5. (  )์€ ์–ด๋–ค ํ”„๋กœ๋ฏธ์Šค๊ฐ€ reject๋œ ์ง€ ์•Œ ์ˆ˜ ์—†์œผ๋ฉฐ, ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” (  )์˜ ์‚ฌ์šฉ์ด ํ•„์š”ํ•˜๋‹ค.
 
6. (  )์€ ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๊ณ , (  )์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
 
7. ๋„ ์˜ต์…”๋„ ์—ฐ์‚ฐ์ž (  )์€ falsy๊ฐ’ ์ค‘ (  )๊ณผ (  )๋งŒ ๋”ฐ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.


 
programming quiz
8.  ๋‹ค์Œ๊ณผ ๊ฐ™์€ Promise๋ฅผ ์‚ฌ์šฉํ•œ ์ˆซ์ž ๋น„๊ต ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์ด ๋•Œ, async/await๋ฅผ ์ด์šฉํ•œ compareNumbersAsync๋ผ๋Š” ์ด๋ฆ„์˜ ์ˆซ์ž ๋น„๊ต ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด, ์„ฑ๊ณตํ–ˆ์„ ๋•Œ์™€ ์‹คํŒจํ–ˆ์„ ๋•Œ์˜ ๊ฐ’์ด ๊ฐ๊ฐ ์ถœ๋ ฅ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค. (๋‹จ, try/catch๋ฌธ์„ ์ด์šฉํ•œ๋‹ค.)

function compareNumbers(a, b) {
    return new Promise((resolve, reject) => {
        if (a > b) {
            resolve("์„ฑ๊ณต!");  // ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๊ฐ€ ๋” ํด ๋•Œ
        } else {
            reject("์‹คํŒจ!");   // ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๊ฐ€ ๋” ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ
        }
    });
}

 
9. ๋‹ค์Œ๊ณผ ๊ฐ™์ด Promise๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์„ ๋•Œ, Promise.allSettled๋ฅผ ์ด์šฉํ•ด ๊ฒฐ๊ด๊ฐ’์ด ์ถœ๋ ฅ๋˜๋„๋ก ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค. (๋‹จ, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค.)

const myPromise1=Promise.resolve('๋‹น์‹ ์€ ์ตœ๊ณ ์˜ˆ์š”.');
const myPromise2=Promise.resolve('๋‹น์‹ ์€ ๋ฉ‹์ ธ์š”.');
const myPromise3=Promise.reject('๋‹น์‹ ์€ ๋Œ€๋‹จํ•ด์š”.');

 


answer
1.var, let, const 

2. ํด๋ž˜์Šค, ํ”„๋กœํ† ํƒ€์ž…
3. ์‹คํ–‰, ๊ฒฐ๊ด๊ฐ’
4. return 
5. Promise.all, Promise.allsettled
6. set, ์ค‘๋ณต
7. ??, null, undefined
8.

function compareNumbers(a, b) {
    return new Promise((resolve, reject) => {
        if (a > b) {
            resolve("์„ฑ๊ณต!");  // ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๊ฐ€ ๋” ํด ๋•Œ
        } else {
            reject("์‹คํŒจ!");   // ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๊ฐ€ ๋” ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ
        }
    });
}

async function compareNumbersAsync(a, b) {
    try {
        const message = await compareNumbers(a, b);  // Promise์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋‹ค๋ฆผ
        console.log(message);  // "์„ฑ๊ณต!" ์ถœ๋ ฅ
    } catch (error) {
        console.error(error);  // "์‹คํŒจ!" ์ถœ๋ ฅ
    }
}

 
9.

const myPromise1=Promise.resolve('๋‹น์‹ ์€ ์ตœ๊ณ ์˜ˆ์š”.');
const myPromise2=Promise.resolve('๋‹น์‹ ์€ ๋ฉ‹์ ธ์š”.');
const myPromise3=Promise.reject('๋‹น์‹ ์€ ๋Œ€๋‹จํ•ด์š”.');
Promise.allSettled([myPromise1,myPromise2,myPromise3])
	.then((result)=>{
    	console.log(result);
    })
    .catch((error)=>{
    	console.error(error);
        });

 
 

์ถœ์ฒ˜) ์กฐํ˜„์ •, ใ€ŒNode.js ๊ต๊ณผ์„œ ๊ฐœ์ • 3ํŒใ€, ๊ธธ๋ฒ—(2022), 2์žฅ ์•Œ์•„๋‘ฌ์•ผ ํ•  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ.

 
 
Corner node.js 2ํŒ€

Editor : ์„ค๋ฆฌ๋ฐ˜

728x90

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ