[TIL]21.06.22

๋ฐ•์ฃผํ™ยท2021๋…„ 6์›” 22์ผ
0

Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
40/104

๐Ÿ‘จโ€๐Ÿ’ป ์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๊ฒƒ

๋™๊ธฐ
: ์ˆœ์ฐจ์ ์œผ๋กœ ์ผ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹
๋น„๋™๊ธฐ
: ์œ ๋‘๋ฆฌ ์žˆ๊ฒŒ ์ผ์„ ๋ฉ€ํ‹ฐํƒœ์Šคํ‚นํ•˜๋ฉฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹

Promise๋ž€ API๊ฐ€ ์ƒ๊ฒจ๋‚œ ์ด์œ 
: ์ฝœ๋ฐฑ์ง€์˜ฅ๋•Œ๋ฌธ..

ํ˜„์žฌ setTimeout์œผ๋กœ ๋น„๋™๊ธฐ๋ฅผ ์˜๋„์ ์œผ๋กœ ๊ตฌํ˜„ํ•จ๋งŒ ๊ณต๋ถ€ํ•œ ํ„ฐ๋ผ ๋น„๋™๊ธฐ๋ฅผ ์ •ํ™•ํžˆ ์–ด๋–ป๊ฒŒ ์จ์•ผ ํšจ์œจ์ ์ผ์ง€ ๋ชฐ๋ผ ์ œ๋Œ€๋กœ๋œ ์˜ˆ์ œ๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ ์ด๋ ‡๋“ฏ ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ์ด๋Ÿฐ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๊ต‰์žฅํžˆ ๋–จ์–ด์ง€๋ฏ€๋กœ ์ƒ๊ฒจ๋‚œ ๊ฒƒ์ด Promise์ด๋‹ค.

const fetchNumber = new Promise((resolve, reject) => {
    setTimeout(() => resolve(1), 1000);
})

fetchNumber
    .then(num => num * 2)
    .then(num => num * 3)
    .then(num => {
        return new Promise((resolve, reject) => {
            setTimeout(() => resolve(num - 1), 1000);
        })
    })
    .then(num => console.log(num));

๋˜ํ•œ ๊ทธ๋Ÿฌํ•œ Promise๋ฅผ ๋” ๊ฐ„ํŽธํ•˜๊ฒŒ ์“ฐ๋Š” ํ‚ค์›Œ๋“œ์ธ async, await ํ‚ค์›Œ๋“œ๋„ ์•Œ์•„๋ดค๋‹ค.

profile
๊ณ ํ†ต์—†๋Š” ์„ฑ์žฅ์€ ์—†๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค....

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