๐Ÿค™ Promise

๊น€์ฒ ์ค€ยท2022๋…„ 1์›” 21์ผ
0

Javascript

๋ชฉ๋ก ๋ณด๊ธฐ
2/16

Promise : ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํŒจํ„ด, ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๋ฉฐ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์‹œ์ ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.



๐Ÿ™„ Why Promise?

Promise๋Š” ์™œ ๋งŒ๋“ค์–ด์กŒ์„๊นŒ?
๊ณ„๊ธฐ๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ์กด ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์ฝœ๋ฐฑํŒจํ„ด์„ ๋จผ์ € ์•Œ์•„๋ด์•ผํ•œ๋‹ค.

๐Ÿ“œ Callback pattern for asynchronous

์ฝœ๋ฐฑ ํŒจํ„ด์€ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.
XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์š”์ฒญ์„ ํ•˜๊ณ  ์‘๋‹ต์„ ๋ฐ›๋Š” ํ˜•ํƒœ์ด๋‹ค.

๋‹ค์Œ์€ XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์š”์ฒญ์„ ํ•˜์—ฌ ์ฝ˜์†”๋กœ ์‘๋‹ต์„ ๋ฐ›๋Š” ์˜ˆ์‹œ์ด๋‹ค.

index.js

const get = (url) => {
  const xhr = new XMLHttpRequest();
  xhr.open("GET", url); //url์— GET ์š”์ฒญ ๋ฉ”์„œ๋“œ๋กœ ๋ณด๋‚ผ๊ฒƒ์ด๋ผ๋Š” ์„ค์ •
  xhr.send(); // ์„œ๋ฒ„์— ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
  xhr.onload = () => {
    //์‘๋‹ต ์ƒํƒœ์— ๋”ฐ๋ฅธ ์‘๋‹ต ๊ฒฐ๊ณผ ์ฝ˜์†”๋กœ ๋ฐ›๊ธฐ
    if (xhr.status === 200) {
      console.log(JSON.parse(xhr.response)); //์‘๋‹ต๊ฒฐ๊ณผ
    } else {
      console.error(`${xhr.status} ${xhr.statusText}`);
    }
  };
};

get("https://jsonplaceholder.typicode.com/posts/1");

console๋กœ ๋ฐ›์€ ์‘๋‹ต ๊ฒฐ๊ณผ

์ด์™€ ๊ฐ™์ด console.log๋ฅผ ํ†ตํ•ด์„œ๋Š” ์‘๋‹ต๊ฒฐ๊ณผ๋ฅผ ์ž˜ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
ํ•˜์ง€๋งŒ ์‘๋‹ต๊ฒฐ๊ณผ๋ฅผ ์‹ค์งˆ์ ์œผ๋กœ ์จ๋จน์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•„๋ณด๊ฒ ๋‹ค.

const get = (url) => {
  const xhr = new XMLHttpRequest();
  xhr.open("GET", url); //url์— GET ์š”์ฒญ ๋ฉ”์„œ๋“œ๋กœ ๋ณด๋‚ผ๊ฒƒ์ด๋ผ๋Š” ์„ค์ •
  xhr.send(); // ์„œ๋ฒ„์— ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
  xhr.onload = () => {
    //์‘๋‹ต ์ƒํƒœ์— ๋”ฐ๋ฅธ ์‘๋‹ต ๊ฒฐ๊ณผ ์ฝ˜์†”๋กœ ๋ฐ›๊ธฐ
    if (xhr.status === 200) {
      return JSON.parse(xhr.response);
    } else {
      console.error(`${xhr.status} ${xhr.statusText}`);
    }
  };
};

const result = get("https://jsonplaceholder.typicode.com/posts/1");
console.log(result); // ์‘๋‹ต๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ๊นŒ?


์•„์‰ฝ๊ฒŒ๋„ ์‘๋‹ต๊ฒฐ๊ณผ๋Š” undefined์ด๋‹ค.
์™œ ์ด๋Ÿฌํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์ผ๊นŒ?

์ด๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด์„  ๋น„๋™๊ธฐ ์ฝ”๋“œ์˜ ์‹คํ–‰ ์‹œ์ ์„ ์•Œ์•„์•ผํ•œ๋‹ค.

โฑ ๋น„๋™๊ธฐ ์ฝ”๋“œ์˜ ์‹คํ–‰ ์‹œ์ 

๋น„๋™๊ธฐ ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์„ ๋•Œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ผ๊ณ ํ•œ๋‹ค.
์œ„ get(url)ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜์ด๋‹ค.
์ด์œ ๋Š” xhr.onload ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋น„๋™๊ธฐ ์ฝ”๋“œ๋Š” ๋ชจ๋“  ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ณ  ๋‚œ ๋’ค์— ์‹คํ–‰์ด ๋œ๋‹ค.
์ฝ”๋“œ๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋  ๋•Œ ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ๋งŒ๋‚˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ์ฝ”๋“œ๋Š” ํƒœ์Šคํฌ ํ๋กœ ์˜ฎ๊ฒจ์ง€๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ณ  ๋‚œ ๋’ค์— ํƒœ์Šคํฌ ํ์—์„œ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋กœ ์˜ฎ๊ฒจ์ ธ ๊ทธ์ œ์„œ์•ผ ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰์ด ๋œ๋‹ค.
xhr.onload ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋Š” ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜์–ด ์ข…๋ฃŒ๊ฐ€ ๋œ ์ดํ›„์— ์‹คํ–‰๋œ๋‹ค๋Š” ์–˜๊ธฐ์ด๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋น„๋™๊ธฐ ํ•จ์ˆ˜์—์„œ ๋ฐ˜ํ™˜๊ฐ’์„ ๋ฐ›์œผ๋ คํ•ด๋„ ๋ฐ›์„ ์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒƒ์ด๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„  ์ฝœ๋ฐฑ ํŒจํ„ด์ด ๋“ฑ์žฅํ•˜๊ฒŒ ๋œ๋‹ค.


๐Ÿง ์ฝœ๋ฐฑ ํŒจํ„ด

๋น„๋™๊ธฐ ํ•จ์ˆ˜์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›๊ธฐ ์œ„ํ•ด์„œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋‚ด๋ถ€์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

index.js

const get = (url, successCallback, failureCallback) => {
  const xhr = new XMLHttpRequest();
  xhr.open("GET", url); //url์— GET ์š”์ฒญ ๋ฉ”์„œ๋“œ๋กœ ๋ณด๋‚ผ๊ฒƒ์ด๋ผ๋Š” ์„ค์ •
  xhr.send(); // ์„œ๋ฒ„์— ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
  xhr.onload = () => {
    //์‘๋‹ต ์ƒํƒœ์— ๋”ฐ๋ฅธ ์‘๋‹ต ๊ฒฐ๊ณผ ์ฝ˜์†”๋กœ ๋ฐ›๊ธฐ
    if (xhr.status === 200) {
      successCallback(JSON.parse(xhr.response));
    } else {
      failureCallback(xhr.status);
    }
  };
};

const p = document.querySelector("p");
get(
  "https://jsonplaceholder.typicode.com/posts/1",
  (success) => {
    p.textContent = JSON.stringify(success); //๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ›๊ธฐ์œ„ํ•˜์—ฌ
  },
  (failure) => {
    p.textContent = JSON.stringify(failure);
  }
);

๋ธŒ๋ผ์šฐ์ € ์‘๋‹ต ๊ฒฐ๊ณผ

getํ•จ์ˆ˜์˜ ์ธ์ˆ˜(successCallback)๋กœ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ์–ด ์‘๋‹ต๊ฒฐ๊ณผ๋ฅผ ๋ฐ›๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด๋‹ค.

ํ•˜์ง€๋งŒ ์œ„์ฒ˜๋Ÿผ ์‘๋‹ต๊ฒฐ๊ณผ๋ฅผ ์ฝœ๋ฐฑํ•จ์ˆ˜์—์„œ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ€๋…์„ฑ์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ์ฝœ๋ฐฑ ์ง€์˜ฅ์ด๋ผ๋Š” ๋ฌธ์ œ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

index.js

const get = (url, successCallback, failureCallback) => {
  const xhr = new XMLHttpRequest();
  xhr.open("GET", url); //url์— GET ์š”์ฒญ ๋ฉ”์„œ๋“œ๋กœ ๋ณด๋‚ผ๊ฒƒ์ด๋ผ๋Š” ์„ค์ •
  xhr.send(); // ์„œ๋ฒ„์— ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
  xhr.onload = () => {
    //์‘๋‹ต ์ƒํƒœ์— ๋”ฐ๋ฅธ ์‘๋‹ต ๊ฒฐ๊ณผ ์ฝ˜์†”๋กœ ๋ฐ›๊ธฐ
    if (xhr.status === 200) {
      successCallback(JSON.parse(xhr.response));
    } else {
      failureCallback(xhr.status);
    }
  };
};

const h1 = document.querySelector("h1");
const p = document.querySelector("p");

get(
  "https://jsonplaceholder.typicode.com/posts/1",
  (success) => {
    h1.textContent = JSON.stringify(success);
    get(
      `https://jsonplaceholder.typicode.com/users/${success.userId}`,
      (userInfo) => {
        p.textContent = JSON.stringify(userInfo);
      }
    );
  },
  (failure) => {
    h1.textContent = JSON.stringify(failure);
  }
);

๋ธŒ๋ผ์šฐ์ € ์‘๋‹ต๊ฒฐ๊ณผ

  • ์œ„ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ์‘๋‹ต๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์„œ ๋‹ค์‹œ ํ•ด๋‹น ์‘๋‹ต๊ฒฐ๊ณผ๋ฅผ ํ™œ์šฉํ•ด์•ผํ•  ๋•Œ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋ฉฐ ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์–ด๋ ค์šด ์ฝœ๋ฐฑ์ง€์˜ฅ์„ ๊ฒช์„ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—๋Ÿฌ์ฒ˜๋ฆฌ๋„ ๊ณค๋ž€ํ•˜๋‹ค๋Š” ๋ฌธ์ œ์ ๋„ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์‘๋‹ต๊ฒฐ๊ณผ๋ฅผ ํ•œ ๋‘๋ฒˆ๋งŒ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•œ๋‹คํ•˜๋ฉด ์ฝœ๋ฐฑํŒจํ„ด๋„ ๋‚˜์˜์ง€์•Š์„ ๊ฒƒ์ด๋‹ค.
๋‹ค๋งŒ promise๊ฐ€ ์กฐ๊ธˆ ๋” ์“ฐ๊ธฐ ํŽธํ•˜๊ณ  ์œ„์™€ ๊ฐ™์€ ๋‹จ์ ์„ ์ปค๋ฒ„ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ๋ฆฌํ•˜์—ฌ ์ด๋Ÿฌํ•œ ์ฝœ๋ฐฑ ํŒจํ„ด์˜ ๋ฌธ์ œ์ ์„ ํ•˜๊ธฐ ์œ„ํ•ด ํƒ„์ƒํ•œ ๊ฒƒ์ด Promise์ด๋‹ค.

์ฝœ๋ฐฑ ํŒจํ„ด์˜ ๋ฌธ์ œ์ 

  • ์ฝœ๋ฐฑ ์ง€์˜ฅ
  • ์—๋Ÿฌ ์ฒ˜๋ฆฌ ๊ณค๋ž€

๐Ÿค™ Promise

Promise๋Š” ES6์— ๋„์ž…๋œ ECMAscript ์‚ฌ์–‘์— ์ •์˜๋œ ๋นŒํŠธ์ธ ๊ฐ์ฒด์ด๋‹ค.

new Promise((resolve,reject) => resolve("success"),reject("failure"))

Description

  • Promise ์ƒ์„ฑ์žํ•จ์ˆ˜๋ฅผ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜๋ฉด ํ”„๋กœ๋ฏธ์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

  • Promise ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์–ด๋– ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํ”„๋กœ๋ฏธ์Šค ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • Promise ๊ฐ์ฒด๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ƒํƒœ์™€ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด์ด๋‹ค.

  • Promise ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›์œผ๋ฉฐ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

  • ์ฒซ๋ฒˆ์งธ ์ฝœ๋ฐฑํ•จ์ˆ˜ resolve๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์„ฑ๊ณตํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
    ์ฝœ๋ฐฑํ•จ์ˆ˜ resolve()์˜ ์ธ์ˆ˜(success)๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์„ฑ๊ณตํ–ˆ์„ ๋•Œ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์ด๋‹ค.

  • ๋‘๋ฒˆ์งธ ์ฝœ๋ฐฑํ•จ์ˆ˜ reject๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํŒจํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
    ์ฝœ๋ฐฑํ•จ์ˆ˜ reject()์˜ ์ธ์ˆ˜๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํŒจํ–ˆ์„ ๋•Œ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์ด๋‹ค.


์ด์ „์˜ ์˜ˆ์ œ๋ฅผ promise๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€๊ฒฝํ•ด๋ณด๊ฒ ๋‹ค.

index.js

const promiseGet = (url) => {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open("Get", url);
    xhr.send();

    xhr.onload = () => {
      if (xhr.status === 200) resolve(JSON.parse(xhr.response));
      else reject(new Error(xhr.status));
    };
  });
};

promiseGet("https://jsonplaceholder.typicode.com/posts/1");

์œ„ ์˜ˆ์ œ์—์„œ๋Š” ๋ณ„ ๋‹ค๋ฅธ ์—๋Ÿฌ๊ฐ€ ์—†์–ด ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์„ฑ๊ณตํ–ˆ์œผ๋‹ˆ resolve()์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‘๋‹ต๊ฐ’์„ promise ๊ฐ์ฒด์— ์ „๋‹ฌํ•œ๋‹ค.

Promise๋Š” ๋น„๋™๊ธฐ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํŒจํ„ด์˜ ์—๋Ÿฌ์ฒ˜๋ฆฌ์™€ ์ฝœ๋ฐฑ ์ง€์˜ฅ์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•ด์ค€๋‹ค๊ณ  ํ•˜์˜€๋‹ค.

promise ๊ฐ์ฒด์˜ ํ›„์†์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ”๋กœ ์ด ๋‹จ์ ๋“ค์„ ๋ณด์™„ํ•ด์ค€๋‹ค.

ํ›„์†์ฒ˜๋ฆฌ๋ฉ”์„œ๋“œ

๋น„๋™๊ธฐ์ฒ˜๋ฆฌ๊ฐ€ ์„ฑ๊ณตํ•˜๋“  (resolve) ์‹คํŒจํ•˜๋“ (reject) ํ”„๋กœ๋ฏธ์Šค๋Š” ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ „๋‹ฌ๋œ ์‘๋‹ต๊ฐ’์€ ํ›„์†์ฒ˜๋ฆฌ๋ฉ”์„œ๋“œ์— ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.

1.then

const promiseGet = (url) => {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open("Get", url);
    xhr.send();

    xhr.onload = () => {
      if (xhr.status === 200) resolve(JSON.parse(xhr.response));
      else reject(new Error(xhr.status));
    };
  }).then((response) => console.log(response));
};

promiseGet("https://jsonplaceholder.typicode.com/posts/1");

  • ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ then์€ ์‘๋‹ต๊ฐ’์„ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.
    (์ฃผ์˜ : ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ ์•„๋‹Œ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์ธ์ˆ˜)

  • ๊ทธ๋ฆฌ๊ณ  then๋ฉ”์„œ๋“œ๋Š” ๋‹ค์‹œ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • then๋ฉ”์„œ๋“œ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด ํ•ด๋‹น ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๊ณ ,
    ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์•”๋ฌต์ ์œผ๋กœ ๊ทธ ๊ฐ’์„ resolve๋‚˜ rejectํ•ด์ฃผ์–ด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    (์–ด์ฐŒ๋ฌ๋“  then()์€ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.)

//1๋ฒˆ
new Promise((res, rej) => {
  res(1);
  rej("error");
}).then(
  (response) =>
    new Promise((res, rej) => {
      res(response);
      rej("error");
    })
);

1๋ฒˆ์˜ˆ์ œ์ฒ˜๋Ÿผ then๋ฉ”์„œ๋“œ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ํ•ด๋‹น Promise๋ฅผ ๋ฐ˜ํ™˜๋ฐ›๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๊ณ ,

//2๋ฒˆ
new Promise((res, rej) => {
  res(1);
  rej("error");
})
  .then((response) => response + 1)
  .then((result) => console.log(result));

2๋ฒˆ์˜ˆ์ œ์ฒ˜๋Ÿผ then๋ฉ”์„œ๋“œ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ promise๊ฐ€ ์•„๋‹Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด๋„ ์•”๋ฌต์ ์œผ๋กœ ํ”„๋กœ๋ฏธ์Šค๋กœ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ๊ณ„์†ํ•ด์„œ ์‘๋‹ต๊ฐ’์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.
์ด๋ฅผ ํ”„๋กœ๋ฏธ์Šค ์ฒด์ด๋‹์ด๋ผํ•œ๋‹ค.

๋˜ํ•œ ์ด๋Ÿฌํ•œ ํ”„๋กœ๋ฏธ์Šค ์ฒด์ด๋‹์ด ๋ฐ”๋กœ ์ฝœ๋ฐฑ ์ง€์˜ฅ์˜ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•ด์ค€๋‹ค.

2.catch

ํ›„์†์ฒ˜๋ฆฌ๋ฉ”์„œ๋“œ catch๋Š” ์—๋Ÿฌ๋ฅผ ์บ์น˜ํ•ด์ค€๋‹ค.

๊ธฐ์กด then๋ฉ”์„œ๋“œ์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํŒจ๋œ ์‘๋‹ต๊ฐ’์„ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” reject(๋‘๋ฒˆ์งธ ์ฝœ๋ฐฑํ•จ์ˆ˜)๋กœ ๋ฐ›์•„์ค˜์•ผํ–ˆ๋‹ค.

new Promise((res, rej) => {
  res(1);
  rej("error");
})

ํ•˜์ง€๋งŒ catch๋Š” reject๋œ ๊ฐ’๋งŒ์„ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.

new Promise((res) => {
  res(1);
})
  .then((result) => console.log(result))
  .catch((e) => console.log(e));
  • ์ด๋Ÿฌํ•œ ์ ์€ ์ฝ”๋“œ๊ฐ€ ๊ฐ€๋…์„ฑ์ด ์ข‹๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.
  • ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ catch๋Š” ์ด์ „ then๋ฉ”์„œ๋“œ๋“ค์˜ ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ์™€ ๋”๋ถˆ์–ด ๋ชจ๋“  then ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๊นŒ์ง€ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

๋•Œ๋ฌธ์— ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์— ์‹คํŒจํ•œ ๊ฐ’์€ catch๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด๋‹ค.

  • catch ๋ฉ”์„œ๋“œ๋„ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

3.finally

  • finally ๋ฉ”์„œ๋“œ๋Š” ๋‹จ ํ•œ๊ฐœ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค.
    ์ด๋Š” resolve์ด๊ฑด reject์ด๊ฑด ๋งˆ์ง€๋ง‰์— ๋ฌด์กฐ๊ฑด ํ•œ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค.

  • ๋˜ํ•œ finally ๋ฉ”์„œ๋“œ๋„ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

new Promise((res) => {
  res(1);
})
  .then((result) => console.log(result))
  .catch((e) => console.log(e))
  .finally(() => console.log("Finally!"));
  • ๋ชจ๋“  ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ ์ด ๋ฉ”์„œ๋“œ๋“ค์€ ๋ชจ๋‘ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

  • ๋˜ํ•œ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ด ๋ฉ”์„œ๋“œ๋“ค์€ ์‘๋‹ต๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•œ๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€