Skip to content

Latest commit

ย 

History

History
716 lines (534 loc) ยท 23.6 KB

step8_exception_handling.md

File metadata and controls

716 lines (534 loc) ยท 23.6 KB

๋ฌธ์„œ ๋ชฉ๋ก์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ

STEP 8

๐Ÿ’ก์งˆ์˜์‘๋‹ต์€ https://github.com/pul8219/TIL Issues ํƒญ์˜ ์•Œ๋งž์€ step ์ด์Šˆ์•ˆ์— ๋‚จ๊ฒจ์ฃผ์„ธ์š”. โžก๏ธ Issueํƒญ์œผ๋กœ ์ด๋™

๋ณด์ถฉ ํ•„์š”

  • ํ”„๋กœํ† ํƒ€์ž…
  • ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ˆœ๊ฐ„ ํ˜ธ์ถœ ์Šคํƒ์˜ ๋™์ž‘ ๋ฐฉ๋ฒ•
  • Promise ์“ธ ๋•Œ ๋‹ค์–‘ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ์˜ˆ์ œ https://gitlab.com/siots-study/topics/-/wikis/exception-handling
  • ์—๋Ÿฌ๊ฐ์ฒด.constructor ๊ฐ์ฒด .constructor .prototype ๋“ฑ ...

์˜ˆ์™ธ ์ฒ˜๋ฆฌ(Exception / Error handling)

Javascript๋Š” ์ฝ”๋“œ ์‹คํ–‰ ์ค‘์— ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ, ์ด๋กœ๋ถ€ํ„ฐ ์ฝ”๋“œ์˜ ์‹คํ–‰ ํ๋ฆ„์„ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ(Exception Handling) ๊ธฐ๋Šฅ์ด ๋‚ด์žฅ๋˜์–ด์žˆ๋‹ค.

(์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์ „์ฒด ์ฝ”๋“œ๋ฅผ ์ค‘๋‹จ์‹œํ‚ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํŠน์ • ์ฝ”๋“œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹คํ–‰๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ! )

ํ”„๋ก ํŠธ์—”๋“œ ๋ฉด์ ‘ ์Šคํ„ฐ๋””, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

JavaScript๋กœ ๋งŒ๋‚˜๋Š” ์„ธ์ƒ, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

์ด ๋ฌธ์„œ๋Š” ์œ„ ๋‘ ๋ฌธ์„œ ๋‚ด์šฉ ์ „๋ฐ˜์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ดํ•ดํ•˜๋ฉด์„œ ๊ฑฐ์˜ ๊ฐ™๊ฒŒ ์ž‘์„ฑํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€์ ์œผ๋กœ ๊ด€๋ จ ๋‚ด์šฉ ํ•™์Šตํ•ด ๋‚˜์˜ ์–ธ์–ด๋กœ ๋ฌธ์„œ ์ˆ˜์ • ํ•„์š”!

๋ชฉ์ฐจ

๋™๊ธฐ์‹ ์ฝ”๋“œ์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

try-catch-finally ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚˜๋”๋ผ๋„ ์ฝ”๋“œ์˜ ์‹คํ–‰์„ ์ง€์†ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. try ๋ธ”๋ก ๋‚ด๋ถ€์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋‹ค๊ฐ€

  2. ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ˆœ๊ฐ„ ์•„์ง ์‹คํ–‰๋˜์ง€ ์•Š์€ ๋‚˜๋จธ์ง€ ์ฝ”๋“œ๋“ค์„ ๋ฌด์‹œ๋œ ์ฑ„ catch ๋ธ”๋ก์œผ๋กœ ์‹คํ–‰ ํ๋ฆ„์ด ๋„˜์–ด๊ฐ„๋‹ค.

catch๋Š” try ๋ธ”๋ก์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ๊ฐ์ฒด๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

finally๋ธ”๋ก ๋‚ด์˜ ์ฝ”๋“œ๋Š” try ๋ธ”๋ก ์•ˆ์—์„œ์˜ ์—๋Ÿฌ ๋ฐœ์ƒ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„ ์—†์ด ๋ฌด์กฐ๊ฑด ์‹คํ–‰๋œ๋‹ค. try ๋ธ”๋ก ๋‚ด์—์„œ return, break, continue ๋“ฑ์œผ๋กœ ์ฝ”๋“œ์˜ ์‹คํ–‰ ํ๋ฆ„์ด ์ด๋™๋  ๋•Œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค.

for (let i of [1, 2, 3]) {
  try {
    if (i === 3) {
      break;
    }
  } finally {
    console.log(`ํ˜„์žฌ i์˜ ๊ฐ’: ${i}`);
  }
}
// ์‹คํ–‰ ๊ฒฐ๊ณผ
// 'ํ˜„์žฌ i์˜ ๊ฐ’: 1'
// 'ํ˜„์žฌ i์˜ ๊ฐ’: 2'
// 'ํ˜„์žฌ i์˜ ๊ฐ’: 3'
  • ์‹คํ–‰๊ฒฐ๊ณผ

image

// try-catch-finally

try {
  console.log('์ž˜ ์‹คํ–‰๋˜๋Š” ์ค‘');
  new Array(-1); // ๐Ÿ
  console.log('์—๋Ÿฌ ๋ฐœ์ƒ ํ›„ ์ฝ”๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š์Œ');
} catch (e) {
  console.log('์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ด ์ฝ”๋“œ์˜ ์‹คํ–‰ ํ๋ฆ„์ด catch ๋ธ”๋ก์œผ๋กœ ์˜ฎ๊ฒจ์™”์Œ');
  console.log(`์—๋Ÿฌ๋ช…: ${e.name} ๋‚ด์šฉ: ${e.message}`);
} finally {
  console.log('finally');
}

// ์‹คํ–‰ ๊ฒฐ๊ณผ
// '์ž˜ ์‹คํ–‰๋˜๋Š” ์ค‘'
// '์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ด ์ฝ”๋“œ์˜ ์‹คํ–‰ ํ๋ฆ„์ด catch ๋ธ”๋ก์œผ๋กœ ์˜ฎ๊ฒจ์™”์Œ'
// '์—๋Ÿฌ๋ช…: RangeError ๋‚ด์šฉ: Invalid array length'
// 'finally'
  • ๐Ÿ ๋ผ์ธ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์„ ๋•Œ ์‹คํ–‰๊ฒฐ๊ณผ

image

์ฆ‰, try-catch-finally ๊ตฌ๋ฌธ์—์„œ ์ฝ”๋“œ์˜ ์‹คํ–‰ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š์•˜์„ ๋•Œ: try - finally

  • ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์„ ๋•Œ: try - ์—๋Ÿฌ ๋ฐœ์ƒ - catch - finally

try..finally

@eyabc ๋‹˜ ๋ฌธ์„œ ์ฐธ๊ณ ํ•˜์—ฌ ์ถ”๊ฐ€

  • ์•ˆ์—์„œ ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„ ๋•Œ
  • ์‹œ์ž‘ํ•œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋งˆ๋ฌด๋ฆฌ ๋˜์—ˆ๋Š”์ง€ ํ™•์‹คํžˆ ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ
function func() {
  // ๋ฌด์–ธ๊ฐ€๋ฅผ ์ธก์ •ํ•˜๋Š” ๊ฒฝ์šฐ์™€ ๊ฐ™์ด ๋๋งบ์Œ์ด ์žˆ์–ด์•ผ ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค
  try {
    // ...
  } finally {
    // ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ฃฝ๋”๋ผ๋„ ์™„๋ฃŒ๋จ
  }
}
  • try ์•ˆ์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋Š” ์™ธ๋ถ€์—์„œ catch ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • finally ๋Š” ์‹คํ–‰ํ๋ฆ„์ด ํ•จ์ˆ˜๋ฅผ ๋– ๋‚˜๊ธฐ ์ „์— ์‹คํ–‰๋œ๋‹ค.

throw ์—ฐ์‚ฐ์ž์™€ ์—๋Ÿฌ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•

Error ์ƒ์„ฑ์ž์™€ throw ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ง์ ‘ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค ์ˆ˜๋„ ์žˆ๋‹ค.

const even = parseInt(prompt('์ง์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”'));

if (even % 2 !== 0) {
  throw new Error('์ง์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.');
}
  • ์‹คํ–‰ ๊ฒฐ๊ณผ

image

throw new Error('์ง์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.');

์œ„ ์ฝ”๋“œ์—์„œ Error ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ์ˆ˜๋กœ ์›ํ•˜๋Š” ์—๋Ÿฌ๋ฉ”์‹œ์ง€ ๋‚ด์šฉ์„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง„ ์—๋Ÿฌ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ๊ทธ ์ฆ‰์‹œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

Error ๋ง๊ณ ๋„ SyntaxError, ReferenceError ๋“ฑ์˜ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ์—๋Ÿฌ ํƒ€์ž…์ด ์žˆ๋‹ค.

๋‹จ ์ธํ„ฐ๋„ท ์ต์Šคํ”Œ๋กœ๋Ÿฌ๋Š” Error() ์ƒ์„ฑ์ž์— ๋Œ€ํ•ด์„œ๋งŒ ์ปค์Šคํ…€ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•œ๋‹ค๊ณ  ํ•˜๋‹ˆ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ปค์Šคํ…€ํ•  ๊ฒฝ์šฐ์—” Error()๋งŒ ์“ฐ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ

๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ์—๋Ÿฌ ๊ฐ์ฒด๋Š” Error ๊ฐ์ฒด๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค. ์ปค์Šคํ…€ ์—๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋„ Error ๊ฐ์ฒด๋ฅผ ์ƒ์†๋ฐ›๋„๋ก ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋งŒ ์ปค์Šคํ…€ํ•˜๋Š” ๊ฒƒ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ปค์Šคํ…€ ์—๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์—๋Ÿฌ์˜ ์ข…๋ฅ˜๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ฑฐ๋‚˜ ์—๋Ÿฌ ๊ฐ์ฒด์— ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ปค์Šคํ…€ ์—๋Ÿฌ๋ฅผ ์“ฐ๋ฉด ์–ด๋””์„œ ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์ž˜ ์•Œ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋””๋ฒ„๊น…์— ์šฉ์ดํ•˜๋‹ค.

class MyError extends Error {
  constructor(value, message, ...params) {
    super(...params);
    this.value = value;
    this.message = message;
    this.name = 'MyError';
  }
}

try {
  const even = parseInt(prompt('์ง์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”'));
  if (even % 2 !== 0) {
    throw new MyError(even, '์ง์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.');
  }
} catch (e) {
  if (e instanceof MyError) {
    console.log(e.value); // ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ •์ˆ˜๊ฐ€ ์ €์žฅ๋œ ๋ณ€์ˆ˜ even์˜ ๊ฐ’
    console.log(e.message); // ์ธ์ˆ˜๋กœ ๋„˜๊ฒจ์ค€ ๋ฉ”์‹œ์ง€ '์ง์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.'
    console.log(e.name); // ์ง€์ •ํ•œ ์—๋Ÿฌ ์ด๋ฆ„ 'MyError'
  }
}

์—๋Ÿฌ ๊ฐ์ฒด์˜ ์ฃผ์š” ํ”„๋กœํผํ‹ฐ

@eyabc ๋‹˜ ๊ธ€ ์ฐธ๊ณ ํ•˜์—ฌ ์ถ”๊ฐ€

  1. name
  • ์—๋Ÿฌ ์ด๋ฆ„
  • 'ReferenceError' : ์ •์˜๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•œ ์—๋Ÿฌ
  1. message
  • ์—๋Ÿฌ ์ƒ์„ธ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฌธ์ž ๋ฉ”์„ธ์ง€
  1. stack
  • ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ๋น„ํ‘œ์ค€ ํ”„๋กœํผํ‹ฐ
  • ํ˜„์žฌ ํ˜ธ์ถœ ์Šคํƒ, ์—๋Ÿฌ๋ฅผ ์œ ๋ฐœํ•œ ์ค‘์ฒฉ ํ˜ธ์ถœ๋“ค์˜ ์ˆœ์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž์—ด
  • ๋””๋ฒ„๊น… ๋ชฉ์ 

๋น„๋™๊ธฐ์‹ ์ฝ”๋“œ์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

๋น„๋™๊ธฐ์‹์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ์ฝœ๋ฐฑ ๋‚ด๋ถ€์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋Š”, ์ฝœ๋ฐฑ ๋ฐ”๊นฅ์— ์žˆ๋Š” try ๋ธ”๋ก์œผ๋กœ๋Š” ์žก์•„๋‚ผ ์ˆ˜ ์—†๋‹ค.

try {
  setTimeout(() => {
    throw new Error('์—๋Ÿฌ!');
  });
} catch (e) {
  console.error(e);
}

์œ„ ์˜ˆ์ œ์—์„œ try ๋ธ”๋ก์€ ์—๋Ÿฌ๋ฅผ ์žก์•„๋‚ด์ง€ ๋ชปํ•˜๊ณ  ์—๋Ÿฌ ๊ฐ์ฒด๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค.

TODO ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ˆœ๊ฐ„ ํ˜ธ์ถœ ์Šคํƒ์˜ ๋™์ž‘ ๋ฐฉ๋ฒ•

JavaScript ์—”์ง„์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ˆœ๊ฐ„ ํ˜ธ์ถœ ์Šคํƒ์„ ๋˜๊ฐ๋Š” ๊ณผ์ •์„ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ • ์ค‘์— try ๋ธ”๋ก์„ ๋งŒ๋‚˜์•ผ ์ฝ”๋“œ์˜ ์‹คํ–‰ ํ๋ฆ„์„ ์›์ƒ๋ณต๊ตฌ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ setTimeout์— ๋„˜๊ฒจ์ง„ ์ฝœ๋ฐฑ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด, ํ˜ธ์ถœ ์Šคํƒ์„ ๋”ฐ๋ผ ์˜ฌ๋ผ๊ฐ€๋„ try ๋ธ”๋ก์„ ๋งŒ๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ฏ€๋กœ, ์ฝ”๋“œ์˜ ์‹คํ–‰ ํ๋ฆ„์ด catch ๋ธ”๋ก์œผ๋กœ ์˜ฎ๊ฒจ์ง€์ง€ ์•Š๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์ œ์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ ์ณ try ๋ธ”๋ก์ด ๋น„๋™๊ธฐ ์ฝœ๋ฐฑ ๋‚ด๋ถ€์— ์œ„์น˜ํ•˜๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค.

setTimeout(() => {
  try {
    throw new Error('์—๋Ÿฌ!');
  } catch (e) {
    console.error(e);
  }
});

Promise

TODO Promise ์“ธ ๋•Œ ๋‹ค์–‘ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ์˜ˆ์ œ https://gitlab.com/siots-study/topics/-/wikis/exception-handling

Promise ๊ฐ์ฒด์˜ ์„ธ ๊ฐ€์ง€ ์ƒํƒœ

  • pending - Promise ๊ฐ์ฒด์— ๊ฒฐ๊ณผ๊ฐ’์ด ์ฑ„์›Œ์ง€์ง€ ์•Š์€ ์ƒํƒœ

  • fulfilled - Promise ๊ฐ์ฒด์— ๊ฒฐ๊ณผ๊ฐ’์ด ์ฑ„์›Œ์ง„ ์ƒํƒœ

  • rejected - Promise ๊ฐ์ฒด์— ๊ฒฐ๊ณผ๊ฐ’์„ ์ฑ„์šฐ๋ ค๊ณ  ์‹œ๋„ํ•˜๋‹ค๊ฐ€ ์—๋Ÿฌ๊ฐ€ ๋‚œ ์ƒํƒœ

Promise ๊ฐ์ฒด๊ฐ€ rejected ์ƒํƒœ๊ฐ€ ๋˜๋ฉด, ์ด Promise์— ๋Œ€ํ•ด์„œ๋Š” then ๋ฉ”์†Œ๋“œ์— ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋„˜๊ฒจ์ค€ ์ฝœ๋ฐฑ์ด ์‹คํ–‰๋˜์ง€ ์•Š๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋„˜๊ฒจ์ค€ ์ฝœ๋ฐฑ์ด ๋Œ€์‹  ์‹คํ–‰๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ฝœ๋ฐฑ์—๋Š” ์—๋Ÿฌ ๊ฐ์ฒด๊ฐ€ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค.

const p = new Promise((resolve) => {
  const even = parseInt(prompt('์ง์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”'));
  if (even % 2 !== 0) {
    throw new Error('์ง์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.');
  } else {
    resolve(even);
  }
});

p.then(
  (even) => {
    return '์ง์ˆ˜์ž…๋‹ˆ๋‹ค.';
  },
  (e) => {
    // ์—๋Ÿฌ์‹œ ์‹คํ–‰๋˜๋Š” ์ฝœ๋ฐฑ(๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋„˜๊ฒจ์ค€ ์ฝœ๋ฐฑ)
    return e.message;
  }
).then(alert);

catch ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์—๋Ÿฌ ์ฒ˜๋ฆฌ ์ฝœ๋ฐฑ์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

p.then((even) => {
  return '์ง์ˆ˜์ž…๋‹ˆ๋‹ค.';
})
  .catch((e) => {
    return e.message;
  })
  .then(alert);

then ๋ฉ”์†Œ๋“œ์˜ ์—ฐ์‡„ ์•ˆ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด, try-catch ๊ตฌ๋ฌธ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ฒ˜์Œ ๋งŒ๋‚˜๋Š” ์—๋Ÿฌ ์ฒ˜๋ฆฌ ์ฝœ๋ฐฑ์œผ๋กœ ์ฝ”๋“œ์˜ ์‹คํ–‰ ํ๋ฆ„์ด ๊ฑด๋„ˆ๋›ฐ๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋‚ณ๊ฒŒ ๋œ๋‹ค.

Promise.resolve()
  .then(() => {
    throw new Error('catch ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.');
  })
  .then(() => {
    console.log('์ด ์ฝ”๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.');
  })
  .catch((e) => {
    return e.message; // ์—ฌ๊ธฐ์„œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ
  })
  .then(console.log);

ํ˜น์€

Promise.resolve()
  .then(() => {
    throw new Error('catch ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.');
  })
  .then(
    () => {
      console.log('์ด ์ฝ”๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.');
    },
    (e) => {
      return '์ด์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค'; // ์—๋Ÿฌ ์ฒ˜๋ฆฌ
    }
  )
  .catch((e) => {
    return e.message; // ์•ž์—์„œ ์—๋Ÿฌ์ฒ˜๋ฆฌ ํ–ˆ์œผ๋‹ˆ ์ด ๋ฉ”์†Œ๋“œ๋Š” ์‹คํ–‰ ์•ˆ๋จ
  })
  .then(console.log);

๋น„๋™๊ธฐ ํ•จ์ˆ˜

์•ž์„œ๋ณธ Promise์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋ฐฉ์‹์€ ๋™๊ธฐ์‹ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฐฉ์‹๊ณผ ๋‹ฌ๋ฆฌ ์ฝœ๋ฐฑ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ง€๋Š” ๊ฒฝํ–ฅ์ด ์žˆ๋‹ค.

๋น„๋™๊ธฐ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋Š” rejected ์ƒํƒœ์˜ Promise ๊ฐ์ฒด๋ฅผ ๋™๊ธฐ์‹ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฐฉ์‹๊ณผ ๋™์ผํ•˜๊ฒŒ try-catch-finally ๊ตฌ๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

async function func() {
  try {
    const res = await fetch('https://nonexistent-domain.nowhere');
  } catch (e) {
    console.log(e.message);
  }
}

func(); // ์‹คํ–‰ ๊ฒฐ๊ณผ: Failed to fetch

๋‹จ, Promise ๊ฐ์ฒด์— ๋Œ€ํ•ด awaitํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ๋ฅผ ์žก์„ ์ˆ˜ ์—†๋‹ค.

async function func() {
  try {
    fetch('https://nonexistent-domain.nowhere');
  } catch (e) {
    console.log(e.message);
  }
}

func(); // ์‹คํ–‰ ๊ฒฐ๊ณผ: ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Œ

์œ„ ์˜ˆ์ œ์—์„œ ์—๋Ÿฌ๋ฅผ ์žก์„ ์ˆ˜ ์—†๋Š” ๊ฒƒ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

  • then ๋ฉ”์†Œ๋“œ์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜ ์ด์šฉ

  • catch ๋ฉ”์†Œ๋“œ ์ด์šฉ

// ์œ„ ์˜ˆ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์—ฌ ์—๋Ÿฌ๋ฅผ ์žก๋Š” ์ฝ”๋“œ
// then ๋ฉ”์†Œ๋“œ์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜ ํ˜น์€ catch ๋ฉ”์†Œ๋“œ๋กœ ํ•ด๊ฒฐ
async function func() {
  fetch('https://nonexistent-domain.nowhere').catch((e) =>
    console.log(e.message)
  );
}

func(); // ์‹คํ–‰ ๊ฒฐ๊ณผ: Failed to fetch

์ถ”๊ฐ€

  • @khw970421 ๋‹˜ ๋‚ด์šฉ ์ถ”๊ฐ€
try {
  console.log('์ž˜ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.');
  console.log(v);
  console.log('๋ฒ„๋ ค์ง');
} catch (e) {
  switch (e.constructor) {
    case SyntaxError:
      console.log('์•ˆ์‹คํ–‰');
      break;
    case ReferenceError:
      console.log(e.constructor);
      console.log('์‹คํ–‰');
      break;
  }
}

์ด๋•Œ e.name์ด ์•„๋‹Œ e.constructor๋ฅผ ์จ์•ผํ•˜๋Š” ์ด์œ ๋Š” e.name์€ String์ด๊ณ  e.constructor๋Š” ์ •ํ™•ํžˆ๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ReferenceError์™€ ๊ฐ™์€ ํƒ€์ž…์ด๊ธฐ๋•Œ๋ฌธ

try {
  console.log('์ž˜ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.');
  console.log(v);
  console.log('๋ฒ„๋ ค์ง');
} catch (e) {
  console.log(e.name === ReferenceError); //false
  console.log(e.constructor === ReferenceError); //true
  console.log(typeof e.constructor); //function
  console.log(typeof ReferenceError); //function
  console.log(toString.call(e.constructor)); //[object Function]
  console.log(toString.call(ReferenceError)); //[object Function]
}

References

ํ”„๋ก ํŠธ์—”๋“œ ๋ฉด์ ‘ ์Šคํ„ฐ๋””, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

JavaScript๋กœ ๋งŒ๋‚˜๋Š” ์„ธ์ƒ, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

Q&A

ํŒ€์›๋“ค ๊ฒฐ๊ณผ๋ฌผ ๋ฐ ์งˆ์˜์‘๋‹ต&์ฝ”๋“œ๋ฆฌ๋ทฐ


์œ ๋ฆผ

[์งˆ๋ฌธ]

//(3) async ์‚ฌ์šฉ ๐Ÿ‘
// async๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ํ•จ์ˆ˜ ์•ˆ์˜ ์ฝ”๋“œ ๋ธ”๋Ÿญ๋“ค์ด ์ž๋™์œผ๋กœ Promise๋กœ ๋ฐ”๋€œ
async function fetchUser() {
  // do network request in 10 secs...
  // ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ค๋Š”๋ฐ 10์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž
  return 'yurim';
}

์—์„œ ์ฝ”๋“œ ๋ธ”๋Ÿญ๋“ค์ด ์ž๋™์œผ๋กœ Promise๋กœ ๋ฐ”๋€œ ์— ๋Œ€ํ•ด์„œ ์ฝ”๋“œ๋ธ”๋ก์ด ์–ด๋–ป๊ฒŒ Promise ๋กœ ๋ฐ”๋€Œ๋Š”์ง€์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ง๋ถ™์—ฌ์ฃผ๋ฉด ์ข‹์„๊ฒƒ๊ฐ™์•„์š”

์ผ๋‹จ์€, ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฐ˜ํ™˜๊ฐ’์€ ํ•ญ์ƒ Promise ๊ฐ์ฒด๋‹ค ๋ผ๋Š” ๋ง์ด ๋” ๋ช…ํ™•ํ•˜์ง€ ์•Š์„๊นŒ์š”?

function ์ด Promise ๊ฐ€ ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด๋„, ์ดํ–‰ ์ƒํƒœ์˜ Promise ๋กœ ๊ฐ์‹ธ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

async function func1() {
  return 1;
}
/*
func1()
Promise {
    [[PromiseState]]: "fulfilled"
    [[PromiseResult]]: 1
}
*/

Promise ์˜ ๋ช…์‹œ์  ๋ฐ˜ํ™˜

๋น„๋™๊ธฐ ํ•จ์ˆ˜ ๋‚ด์—์„œ return ํ•œ ๊ฐ’์ด, Promise ๊ฐ์ฒด์˜ [[PromiseResult]] ๊ฐ’

async function func2() {
  return Promise.resolve(2);
}
/*
func2()
Promise {
    [[PromiseState]]: "fulfilled"
    [[PromiseResult]]: 2
}
*/

async function func4() {
  return Promise.reject(2);
}

/* 
func4()
Promise {
    [[PromiseState]]: "rejected"
    [[PromiseResult]]: 2
}
*/

๋น„๋™๊ธฐ ํ•จ์ˆ˜์—์„œ return ์„ ํ•ด์ฃผ์ง€ ์•Š์•˜์„ ๋•Œ,

async function func3() {}
/*
func3()
Promise {
    [[PromiseState]]: "fulfilled"
    [[PromiseResult]]: undefined
}
*/

๋น„๋™๊ธฐ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์— ๋Œ€ํ•ด์„œ ์‹ ๊ฒฝ์„ ์จ๋ณด์ง€ ์•Š์•˜๋Š”๋ฐ, ์œ ๋ฆผ๋‹˜ ๊ธ€ ์ฝ๊ณ  ์ •๋ฆฌํ•˜๋ฉด์„œ ๋‹ค์‹œํ•œ๋ฒˆ ๊ณต๋ถ€ํ•˜๊ฒŒ ๋์–ด์š”!


๋นต๊ณผ ์ปคํ”ผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜์˜ ์˜ˆ์ œ๊ฐ€ ์ธ์ƒ๊นŠ์–ด์š”!

async function getBread() {
  await delay(1000); // 1์ดˆ๋ฅผ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€
  return '๐Ÿฅ'; //๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” Promise๊ฐ€ ๋งŒ๋“ค์–ด์ง
}

async function getCoffee() {
  await delay(1000); // 1์ดˆ๋ฅผ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€
  return 'โ˜•๏ธ'; //๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” Promise๊ฐ€ ๋งŒ๋“ค์–ด์ง
}
// 1) Promise.all
// Promise ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•˜๋ฉด, ๋ชจ๋“  Promise๋“ค์ด ๋ณ‘๋ ฌ์ ์œผ๋กœ ๋‹ค ๋‹ด๊ธธ๋•Œ๊นŒ์ง€ ๋ชจ์•„์ฃผ๋Š” ์นœ๊ตฌ. ๋‹ด๊ธธ๋•Œ๋„ ๋ฐฐ์—ด๋กœ ๋‹ด๊น€

์˜ ์„ค๋ช…์—์„œ ์ถ”๊ฐ€์ ์œผ๋กœ iterable ์—์„œ reject ๊ฐ€ ๋ฐœ์ƒํ•ด๋„ ์ทจ์†Œ๋˜์ง€ ์•Š๊ณ , ๋‚˜๋จธ์ง€ Promise ๋„ ๊ฒฐ๊ณผ๋ฅผ ์‚ฐ์ถœํ•˜์ง€๋งŒ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค

const mixedPromisesArray = [Promise.resolve(33), Promise.reject(44)];
const p = Promise.all(mixedPromisesArray); // (1) Promise { <state>: "pending" }
console.log(p); // (3) Promise { <state>: "rejected", <reason>: 44 }
setTimeout(function () {
  console.log('the stack is now empty'); // (2) the stack is now empty
  console.log(p);
});

[๋‹ต๋ณ€]

@eyabc

๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฐ˜ํ™˜๊ฐ’์€ ํ•ญ์ƒ Promise ๊ฐ์ฒด๋‹ค

์€์˜๋‹˜ ๋ง์ฒ˜๋Ÿผ ์ด ๋ง์ด ํ›จ์”ฌ ๋ช…ํ™•ํ•˜๋„ค์š”! ์ฝ”๋“œ ์˜ˆ์ œ๊นŒ์ง€ ์–ธ๊ธ‰ํ•˜์—ฌ ๋ฆฌ๋ทฐํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜์ •๋ณด์™„ํ• ๊ฒŒ์š”! ๋งˆ์ง€๋ง‰์— Promise.all ๊ด€๋ จํ•˜์—ฌ reject๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ๋Š” ๊ทธ๋Œ€๋กœ ์ง„ํ–‰๋˜์ง€๋งŒ Promise ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‹ด๊ธด ์š”์†Œ๋“ค๋„ ๋ฉ๋‹ฌ์•„ ๋‹ด๊ธฐ์ง€ ์•Š๋Š”๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ƒ๊ฐ์ง€ ๋ชปํ–ˆ๋„ค์š”... ๋‚ด์šฉ ์ข€ ์ฐธ๊ณ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค

ํฌ๋กœ์™€์ƒ๊ณผ ์ปคํ”ผ์•„์ด์ฝ˜์€ VSCode์—์„œ emoji ์“ฐ๋Š” ๊ฑธ ์ด์ œ ์•Œ์•„์„œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ์‹ ๋‚˜์„œ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ์–ผ๋ฅธ ์ฝ”๋กœ๋กฑ์ด ์ข€ ์ž ์ž ํ•ด์ ธ์„œ ์Šคํ„ฐ๋”” ์˜คํ”„๋ชจ์ž„ ๊ฐ€์ง€๋ฉด ๊ฐ™์ด ์ปคํ”ผ๋ž‘ ๋นต ๋จน๊ณ  ์‹ถ๋„ค์š” ๐Ÿง


์€์˜

to ์€์˜

[์งˆ๋ฌธ]

๋งˆ์ง€๋ง‰์— ๋ถ€๋žด๋ถ€๋žด ํ•˜๋Š๋ผ ์ œ ๊ฒฐ๊ณผ๋ฌผ ์˜ˆ์™ธ์ฒ˜๋ฆฌ์ชฝ ๋‚ด์šฉ์ด ๋ถ€์‹คํ•œ๋ฐ ์€์˜๋‹˜ ๊ธ€์ด ๋งŽ์ด ๋„์›€์ด ๋˜์–ด์„œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€/์ˆ˜์ •ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์—๋Ÿฌ ๋‹ค์‹œ ๋˜์ง€๊ธฐ

๋ถ€๋ถ„์—์„œ ์•„๋ž˜ ์ฝ”๋“œ๊ฐ€ user ์•ž์— let์„ ๋ถ™์ด๋Š” ๊ฑธ ์žŠ์–ด ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๋ฅผ ์„ค๋ช…ํ•ด์ฃผ์…จ๋Š”๋ฐ์š” ์ด๋ ‡๊ฒŒ let์œผ๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋ฅผ ๋ณ€์ˆ˜ ์„ ์–ธ ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ์ง€ ์•Š๊ณ  ์ „์—ญ๋ณ€์ˆ˜(?)๋กœ ๋ฐ›์•„๋ฒ„๋ฆฌ๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„  ๋ฌด์กฐ๊ฑด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋‚˜์š”?

let json = '{ "age": 30 }'; // ๋ถˆ์™„์ „ํ•œ ๋ฐ์ดํ„ฐ

try {
  user = JSON.parse(json); // <-- user ์•ž์— let์„ ๋ถ™์ด๋Š” ๊ฑธ ์žŠ์—ˆ๋„ค์š”.
  // ...
} catch (err) {
  alert('JSON Error: ' + err); // JSON Error: ReferenceError: user is not defined
  // (์‹ค์ œ๋ก  JSON Error๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.)
}

๊ฐ™์€ ํŒŒํŠธ์— ์•„๋ž˜ ๋‚˜์™€์žˆ๋Š” ์ฝ”๋“œ ์˜ˆ์ œ์—์„œ์š”, user ์•ž์— let์„ ๋ถ™์ด์ง€ ์•Š์•„ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ์ธ๋ฐ ์ฝ”๋“œ์˜ user ์•ž์— let์ด ์™œ ๋ถ™์–ด์žˆ๋Š”๊ฑด๊ฐ€์š”...?!

user ์•ž์— let ์„ ๋ถ™์ด์ง€ ์•Š์•„ ๋ฐœ์ƒํ•˜๋Š” ReferenceError ๋Š” ์™ธ๋ถ€์—์„œ ์ฒ˜๋ฆฌํ•˜๋„๋ก catch ๋ธ”๋ก์—์„œ throw ๋ฅผ ํ•œ๋ฒˆ ๋” ํ•ด์ค€๋‹ค.

let json = '{ "age": 30 }'; // ๋ถˆ์™„์ „ํ•œ ๋ฐ์ดํ„ฐ
try {
  let user = JSON.parse(json);
  if (!user.name) throw new SyntaxError('๋ถˆ์™„์ „ํ•œ ๋ฐ์ดํ„ฐ: ์ด๋ฆ„ ์—†์Œ');
  blabla(); // ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์—๋Ÿฌ
  alert(user.name);
} catch (e) {
  if (e instanceof SyntaxError) {
    alert('JSON Error: ' + e.message);
  } else {
    throw e; // ์—๋Ÿฌ ๋‹ค์‹œ ๋˜์ง€๊ธฐ (*)
  }
}

[๋‹ต๋ณ€]

@pul8219

const json = '{ "age": 30 }'; // ๋ถˆ์™„์ „ํ•œ ๋ฐ์ดํ„ฐ

try {
  user = JSON.parse(json); // <-- user ์•ž์— let์„ ๋ถ™์ด๋Š” ๊ฑธ ์žŠ์—ˆ๋„ค์š”.
  // ...
} catch (err) {
  alert('JSON Error: ' + err); // JSON Error: ReferenceError: user is not defined
  // (์‹ค์ œ๋ก  JSON Error๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.)
}

์œผ๋กœ ์ •์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰ํ•  ๋•Œ์—๋Š” 'use strict' ๋ฅผ ๋งจ์•ž์— ๋ถ™์—ฌ์ฃผ์…”์•ผ ์ „์—ญ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ „์—ญ ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ ๋ ๋•Œ๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์ง€๋งŒ, ES6 ๋ถ€ํ„ฐ๋Š” ๋ณ€์ˆ˜์•ž์— ํ‚ค์›Œ๋“œ(const, let, var )๋ฅผ ์„ ์–ธํ•ด ์ฃผ์ง€ ์•Š์œผ๋ฉด ์ „์—ญ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉ ๋˜์ง€ ์•Š๊ณ  Reference ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค

์ œ๊ฐ€ ์•„๋ฌด์ƒ๊ฐ์—†์ด ๊ณต๋ถ€ํ•˜๋‹ค๋ณด๋‹ˆ ์‹ค์ˆ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋„ค์š” ์ •์ •๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!


ํ˜•์šฑ

https://github.com/khw970421/js-interview/blob/master/const/project8.md

by ํ˜•์šฑ

[์งˆ๋ฌธ]

๋‚ด์šฉ ๊ฐ„๊ฒฐํ•˜๋ฉด์„œ ํ•„์š”ํ•œ๋ถ€๋ถ„์„ ์ดํ•ดํ• ์ˆ˜์žˆ์–ด์„œ ์ข‹์•˜์Šต๋‹ˆ๋‹ค.
๋•๋ถ„์— Promise๋ถ€๋ถ„ ์ˆ˜์ •ํ•˜๋Š”๋ฐ ์ข€ ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
์ฒ˜์Œ ์ฝ”๋“œ๋ถ€๋ถ„์—์„œ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ ์„œ ๊ฐ™์€ ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๋Š”๊ฒŒ ์ € ๋˜ํ•œ ํ‰์†Œ์— ๊ตฌ๋ถ„์ง€์–ด์„œ ์ฐจ์ด์ ์„ ์•Œ์•„๋ณด๋Š”๊ฒƒ์ด ์ต์ˆ™ํ•ด ์ฐจ์ด์ ์„ ๋ณด๋Š”๋ฐ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ์Šต๋‹ˆ๋‹ค.
Promise๊ด€๋ จํ•ด์„œ https://github.com/khw970421/js-interview/blob/master/const/project8.md ์ €์˜ ์ •๋ฆฌ๋„ ๋„์›€์ด ๋˜์‹œ์—ˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค.

[๋‹ต๋ณ€] @khw970421 ๋ฆฌ๋ทฐ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ํ‰์†Œ ํ˜•์šฑ๋‹˜ ํ•˜์‹œ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ๊ฒฝ์šฐ๋ณ„๋กœ ๋‚˜๋ˆ„๊ฑฐ๋‚˜ ๋ฐœ์ „์‹œํ‚ค๋Š”๊ฒŒ ์ดํ•ดํ•˜๋Š”๋ฐ ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š”! ํ˜•์šฑ๋‹˜ ์ •๋ฆฌ๋„ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค ๋ฌธ์„œ ์ˆ˜์ •์— ์ฐธ๊ณ ํ• ๊ฒŒ์š”~

to ํ˜•์šฑ

[์งˆ๋ฌธ]

๋งˆํฌ๋‹ค์šด์œผ๋กœ ์ž‘์„ฑํ•˜์…จ๊ตฐ์š”! ์›๋ž˜๋„ ์ข‹์•˜๋Š”๋ฐ ๊ฐ€๋…์„ฑ์ด ๋”๋” ์ข‹์•„์ง„ ๊ฒƒ ๊ฐ™์•„์š” step8 ์ž˜ ์ฝ์—ˆ์Šต๋‹ˆ๋‹ค ๐Ÿ‘ ์ œ ๊ฒฐ๊ณผ๋ฌผ์— ๋‚จ๊ฒจ์ฃผ์‹  ๋ฆฌ๋ทฐ๋ณด๊ณ  ํ˜•์šฑ๋‹˜ ๋ฌธ์„œ์ค‘ Promise ๋ถ€๋ถ„๋„ ์ •๋…ํ–ˆ์Šต๋‹ˆ๋‹ค.

[์งˆ๋ฌธ-์€์˜]

์—๋Ÿฌ์—๋Š” ๋ฌธ๋ฒ• ์—๋Ÿฌ, ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ, ๋…ผ๋ฆฌ์  ์—๋Ÿฌ๊ฐ€ ์žˆ๋””.

๋ฌธ๋ฒ• ์—๋Ÿฌ
console.log(;
๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ

๋ฐฐ์—ด ์ธ๋ฑ์Šค ๋ฒ”์œ„
์ž˜๋ชป๋œ ๊ณต๊ฐ„์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ
0์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ
๋…ผ๋ฆฌ์  ์—๋Ÿฌ

์‹คํ–‰์‹œ ์˜ค๋ฅ˜x
์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹๋•Œ

์—๋Ÿฌ์˜ ์ข…๋ฅ˜์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜์‹  ์ ์ด ์ž˜ํ•˜์‹ ๊ฒƒ ๊ฐ™์•„์š”

constructor ๋ฅผ ํ†ตํ•ด์„œ ์—๋Ÿฌ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฐ์š”.

try {
  console.log('์ž˜ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.');
  console.log(v);
  console.log('๋ฒ„๋ ค์ง');
} catch (e) {
  switch (e.constructor) {
    case SyntaxError:
      console.log('์•ˆ์‹คํ–‰');
      break;
    case ReferenceError:
      console.log(e.constructor);
      console.log('์‹คํ–‰');
      break;
  }
}

์ถ”๊ฐ€์ ์œผ๋กœ if (e instanceof SyntaxError) ์ด์ฒ˜๋Ÿผ instanceof ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด SyntaxError ๋ฅผ ์ƒ์†ํ•œ ์—๋Ÿฌ๋„ ์žก์„ ์ˆ˜ ์žˆ๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค.


๋…ธ์›

https://github.com/quavious/hell_script/blob/master/chapter8.js

[์งˆ๋ฌธ-์€์˜]

console.dir ๋ฉ”์†Œ๋“œ๋Š” ๋ช…์‹œ๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์˜ ์ „๋ฐ˜์ ์ธ ์ •๋ณด๋ฅผ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ์ถœ๋ ฅํ•ด์ค€๋‹ค.
์›น์—์„œ๋งŒ ์ •๋ณด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. NodeJS์—์„œ ์‹คํ–‰์‹œ log ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

node ์—์„œ console.dir ์€ console.log ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๊ตฐ์š”

์ •์›…

https://jeongshin.github.io/JeongShin_Blog/TIL/study/JavaScript2.html#step-8-%F0%9F%91%89-error-handling

[์งˆ๋ฌธ-์€์˜]

ES6์— ๋“ฑ์žฅํ•œ Promise ๋ฅผ ์ด์šฉํ•œ ๋ฐฉ์‹์€ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•œ Promise ์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœํ•  ๋•Œ ๋ฐ”๋กœ ์‹คํ–‰๋˜์ง€๋งŒ
๊ทธ ๋‚ด๋ถ€์— resolve ๋˜๋Š” reject ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ตฌ๋ฌธ์ด ์žˆ์„ ๊ฒฝ์šฐ ๋‘˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” then ๋˜๋Š” catch ๊ตฌ๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋Š”๋‹ค.