๐ŸŒ Promises / Async&Await

๐Ÿ‘พยท2021๋…„ 7์›” 26์ผ
0

HTTP

๋ชฉ๋ก ๋ณด๊ธฐ
4/4

๋น„๋™๊ธฐ ๊ฐœ๋… : https://developer.mozilla.org/ko/docs/Learn/JavaScript/Asynchronous/Concepts

์ถœ์ฒ˜ : MDN Web Docs

Promise๋ž€?

์–ด๋–ค ์ž‘์—…์˜ ์ค‘๊ฐ„์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด. ๋ฏธ๋ž˜์— ์–ด๋–ค ์ข…๋ฅ˜์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋จ์„ ์•ฝ์†ํ•œ๋‹ค.

Promise ๊ฐ์ฒด๋Š” ๋น„๋™๊ธฐ ์ž‘์—…์ด ์ข…๋ฃŒ๋œ ์ดํ›„์— ๊ฒฐ๊ณผ๊ฐ’์ด๋‚˜ ์‹คํŒจ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
Promise๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋น„๋™๊ธฐ ๋ฉ”์„œ๋“œ์—์„œ ๋งˆ์น˜ ๋™๊ธฐ๋ฉ”์„œ๋“œ์ฒ˜๋Ÿผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ promise๋ฅผ ๋ฐ˜ํ™˜ํ•ด์„œ ๋ฏธ๋ž˜์˜ ์–ด๋–ค ์‹œ์ ์— ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

Promise๊ฐ€ ๊ฐ€์ง€๋Š” 3๊ฐ€์ง€ ์ƒํƒœ

1. pending : fulfilled๋˜๊ฑฐ๋‚˜ rejected๋˜์ง€ ์•Š์€ ์ดˆ๊ธฐ ์ƒํƒœ
2. fulfilled : ์—ฐ์‚ฐ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋จ
3. rejected : ์—ฐ์‚ฐ์ด ์‹คํŒจํ•จ.

pending์ค‘์ธ promise๋Š” ๊ฐ’๊ณผ ํ•จ๊ป˜ fulfilled๋ ์ˆ˜๋„ ์žˆ๊ณ , ์˜ค๋ฅ˜๋กœ ์ธํ•ด rejected๋ ์ˆ˜๋„ ์žˆ๋‹ค.

  • fulfilled์ƒํƒœ๊ฐ€ ๋˜๋ฉด .then()๋ธ”๋Ÿญ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • rejected์ƒํƒœ๊ฐ€ ๋˜๋ฉด ์–ด๋–ค ์ด์œ  ๋•Œ๋ฌธ์— rejected๋˜์—ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์—๋Ÿฌ๋ฉ”์„ธ์ง€๋ฅผ ํฌํ•จํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
//์ƒˆ ํ”„๋กœ๋ฏธ์Šค ์ƒ์„ฑ
let myFirstPromise = new Promise(
  //์‹คํ–‰ํ•จ์ˆ˜๋Š” promsie๋ฅผ resolveํ•˜๊ฑฐ๋‚˜ rejectํ•  ์ˆ˜ ์žˆ๋‹ค.
  (resolve, reject) => {
  //๋น„๋™๊ธฐ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ณด์ด๊ธฐ ์œ„ํ•ด setTimeout ์‚ฌ์šฉ
  setTimeout(function(){
    resolve("Success!"); // Yay! Everything went well!
  }, 250);
});

//promise๋ฅผ fulfilledํ–ˆ์„๋•Œ ํ•  ์ผ์€ then()์œผ๋กœ ์ •์˜, ๊ฑฐ๋ถ€๋˜์—ˆ์„๋•Œ ํ•  ์ผ์€ catch()๋กœ ์ •์˜
myFirstPromise.then(
  //์ดํ–‰ ๊ฐ’ ์ž‘์„ฑ
  (successMessage) => {
  console.log("Yay! " + successMessage);
})
.catch(
  //๊ฑฐ๋ถ€ ์ด์œ  ๊ธฐ๋ก
  function(reason){
    console.log('์—ฌ๊ธฐ์„œ ๊ฑฐ๋ถ€๋œ promise('+reason+')๋ฅผ ์ฒ˜๋ฆฌํ•˜์„ธ์š”.');
  });
}

  • finally()
    promise ์ฒด์ด๋‹์˜ ๋์— ๋ฐฐ์น˜. ๊ฒฐ๊ณผ๊ฐ€ fulfilled์ธ์ง€ rejected์ธ์ง€ ๊ด€๊ณ„์—†์ด promise๊ฐ€ ์™„๋ฃŒ๋œ ํ›„ ์ตœ์ข… ์ฝ”๋“œ ๋ธ”๋Ÿญ์„ ์‹คํ–‰ํ•˜๋ ค๊ณ  ํ• ๋•Œ ์‚ฌ์šฉ.
myPromise
.then(response => {
  doSomething(response);
})
.catch(e => {
  returnError(e);
})
.finally(() => {
  runFinalCode();
});

Async & Await

async

function()์•ž์— async๋ฅผ ์ถ”๊ฐ€ -> await๊ฐ€ ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜.

async function hello() { return "Hello" };
hello(); //promise๋ฅผ ๋ฐ˜ํ™˜.
hello().then((value)=>console.log(value));

async๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•œ๋‹ค.

await

async function hello() {
  return greeting = await Promise.resolve("Hello");
};

hello().then(alert);

await๋Š” async ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ.
.then()๋ธ”๋Ÿญ ๋Œ€์‹  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „์— await๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹น.
await๋Š” ์ด ๋ผ์ธ์—์„œ ์ผ์‹œ์ค‘์ง€ํ•˜์—ฌ ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ• ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒํ•œ๋‹ค.
fulfilled๋œ promise์—์„œ ๋ฐ˜ํ™˜๋œ ์‘๋‹ต์€ greeting๋ณ€์ˆ˜์— ํ• ๋‹น๋œ๋‹ค.

์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ

try...catch ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ

async function myFetch() {
  try {
    let response = await fetch('coffee.jpg');

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    let myBlob = await response.blob();
    let objectURL = URL.createObjectURL(myBlob);
    let image = document.createElement('img');
    image.src = objectURL;
    document.body.appendChild(image);

  } catch(e) {
    console.log(e);
  }
}

myFetch();

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