MapleStory Finger Point

๐ŸŸก JavaScript

[JS ES6] JS ES6 ๋ฌธ๋ฒ• Promise, async, await

HYEJU01 2024. 8. 29. 22:02


ํ”„๋กœ๋ฏธ์Šค๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝ์— ๋‚ด์žฅ๋œ ๋‚ด์žฅ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค
ํ”„๋กœ๋ฏธ์Šค ๊ฐ์ฒด์—์„œ๋Š” 2๊ฐ€์ง€๋ฅผ ํ˜•ํƒœ๋ฅผ ์ง€๋‹™๋‹ˆ๋‹ค.

 

1. ์ƒํƒœ(state) 

  • pending : ์ˆ˜ํ–‰์ค‘
  • fullfilled : ์„ฑ๊ณต์  ์™„๋ฃŒ
  • rejected : ์‹คํŒจ

2. ํ”„๋กœ๋“€์„œ - ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ œ๊ณต์ž(์ฆ‰, Promise)
    ์ปจ์Šˆ๋จธ - ์‚ฌ์šฉ์ž(์ฆ‰, ํ˜ธ์ถœํ•˜๋Š”์‚ฌ๋žŒ)

 


ํ”„๋กœ๋ฏธ์Šค ์ƒ์„ฑ
executor์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•˜๋ฉฐ executor์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ๋‹ค์‹œ resolveํ•จ์ˆ˜์™€ rejectํ•จ์ˆ˜๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.

//๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ปจ์Šˆ๋จธ๊ฐ€ ๋ถ„๋ช… ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ• ๊ฑฐ์•ผ!(๊ทธ ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ๋ฆฌํ„ดํ•ด์ค„๊ฒŒ!)
let promise = new Promise( (success, fail) => {

    //producer ์ž…์žฅ์—์„œ ์„ฑ๊ณตํ–ˆ์„ ๋•Œ ์„ฑ๊ณต์„ ์•Œ๋ฆฌ๋Š” ํ•จ์ˆ˜
    success("success"); 

    //producer ์ž…์žฅ์—์„œ ์‹คํŒจํ–ˆ์„ ๋•Œ ์‹คํŒจ๋ฅผ ์•Œ๋ฆฌ๋Š” ํ•จ์ˆ˜
    //fail(new Error("fail"));

});

console.log(promise)

ํ”„๋กœ๋ฏธ์Šค์˜ ์‚ฌ์šฉ then(), catch()

  • then์€ promise๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰ ๋œ๋‹ค๋ฉด, ๊ฒฐ๊ณผ๋ฅผ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜์ด๋‹ค.
  • catch๋Š” promise๊ฐ€ ์‹คํŒจํ–ˆ์„ ๋•Œ, ๊ฒฐ๊ณผ๋ฅผ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜์ด๋‹ค.
//....์ƒ๋žต
promise.then( data => console.log(data) )
       .catch( error => console.log(error) )

ํ”„๋กœ๋ฏธ์Šค์˜ ์ ์šฉ

๊ฒฐ๊ณผ๊ฐ€ ์–ธ์ œ ๋Œ์•„๊ฐˆ์ง€ ๋ชจ๋ฅด์ง€๋งŒ, ๋‚ด๊ฐ€ ์ด๊ฒƒ์„ ์™„๋ฃŒ๋˜๋ฉด Promise๋ฅผ ์ „๋‹ฌํ•ด ์ค„ํ…Œ๋‹ˆ, ๋„ˆ๋Š” thenํ•จ์ˆ˜๋งŒ ์ด์šฉํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•ด!

 

ajax fetch()์˜ ๋‚ด๋ถ€๋ชจ์Šต ์˜ˆ์‹œ

//์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ๋Š” fetch๋ผ๊ณ  ๊ฐ€์ •
function myfetch(req) { 
    //๋น„๋™๊ธฐ์  ์‹คํ–‰.. 10์ดˆ..
    return new Promise( (success, fail) => {
        success("data....");
    });
}

//fetch์™€ ๊ฐ™์€ ์‹คํ–‰๊ตฌ์กฐ๋ฅผ ๋ณด์ธ๋‹ค.
myfetch("http://localhost:5502/~~~~~").then( response => console.log(response) );

 


Promise๋ฅผ ๋” ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” async(์ด๊ฑฐ ๋น„๋™๊ธฐ์•ผ)

async, await๊ทœ์น™

  1. ์–ด์‹ฑํฌ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์–ด์›จ์ž‡์„ ์‚ฌ์šฉํ•œ๋‹ค
  2. function ์•ž์— async ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€ ํ•จ์ˆ˜๋Š” ์–ธ์ œ๋‚˜ ํ”„๋ผ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ฆฌํ„ด์ด ํ”„๋กœ๋ฏธ์Šค๋ผ๋ฉด await์„ ์ ์šฉํ•˜๊ณ  then์ ˆ์„ ์—†์•จ ์ˆ˜ ์žˆ๋‹ค.

 

async๋ž€ function์•ž์— ์„ ์–ธํ•˜๋ฉฐ, return๊ฐ’์„ ์ž๋™์œผ๋กœ Promise๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
์ฆ‰, ๋น„๋™๊ธฐ ํ•จ์ˆ˜์˜ ์‹คํ–‰ ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

async function myfetch(req) {
    //๋น„๋™๊ธฐ์  ์‹คํ–‰.. 10์ดˆ..
    return "data....";
}


//var result = myfetch()
//console.log(result); //Promise

myfetch("http://localhost:5502/~~~~~").then( response => console.log(response) );

 

Promise๋ฅผ ๋” ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” await(๊ธฐ๋‹ค๋ ค)

๋ฆฌํ„ด์ด ํ”„๋กœ๋ฏธ์Šค๋ผ๋ฉด await์„ ์ ์šฉํ•˜๊ณ  then์ ˆ์„ ์—†์•จ ์ˆ˜ ์žˆ๋‹ค. (๋‹จ asyncํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค)

(async function() {
    let data = await myfetch();
    console.log(data)

})();

async, await์˜ ์žฅ์ 

  1. ์ฝ”๋“œ์˜ ๊ฐ„๊ฒฐ์„ฑ
  2. ์–ด์‹ฑํฌ, ์–ด์›จ์ž‡์€ ๋™๊ธฐ์ ๋ฐฉ์‹(์ˆœ์„œ๋ฅผ) ๋ณด์žฅํ•œ๋‹ค

JS ES6 ๋ฌธ๋ฒ• Promise, async, await