JavaScript

Promise

meteorqz6 2025. 3. 16. 16:46

비동기 μ½”λ“œμ—μ„œλŠ” μ‹€ν–‰ μˆœμ„œλ₯Ό μ˜ˆμΈ‘ν•˜κΈ° μ–΄λ ΅κΈ° λ•Œλ¬Έμ— 비동기 μž‘μ—…μ΄ λλ‚œ ν›„ μ‹€ν–‰ν•  μ½”λ“œλ₯Ό λͺ…ν™•ν•˜κ²Œ μ œμ–΄ν•˜κΈ° μœ„ν•΄μ„œ Promiseκ°€ ν•„μš”ν•©λ‹ˆλ‹€.

Promise λž€?

비동기 μž‘μ—…μ„ 효율적으둜 μ²˜λ¦¬ν•  수 μžˆλ„λ‘ λ„μ™€μ£ΌλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ λ‚΄μž₯ 객체

Promise의 3κ°€μ§€ μƒνƒœ

- Pending(λŒ€κΈ°) : 아직 μž‘μ—…μ΄ μ™„λ£Œλ˜μ§€ μ•Šμ€ μƒνƒœ

- Fulfilled(성곡) : 비동기 μž‘μ—…μ΄ μ„±κ³΅μ μœΌλ‘œ 마무리된 μƒνƒœ

- Rejected(μ‹€νŒ¨) : 비동기 μž‘μ—…μ΄ μ‹€νŒ¨ν•œ μƒνƒœ

 

λŒ€κΈ° -> 성곡 : ν•΄κ²°(resolve) - μ›ν•˜λŠ” μž‘μ—…μ΄ μ™„λ£Œλ˜μ—ˆμ„ λ•Œ μ‹€ν–‰

λŒ€κΈ° -> μ‹€νŒ¨ : κ±°λΆ€(reject) - μž‘μ—… 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμ„ λ•Œ μ‹€ν–‰

 

const promise = new Promise(() => {
	setTimeout(() => {
    	console.log("μ•ˆλ…•");
    }, 2000);
});

console.log(promise);

 

const promise = new Promise((resolve, reject) => {
	setTimeout(() => {
    	console.log("μ•ˆλ…•");
        resolve();
    }, 2000);
});

setTimeout(() => {
	console.log(promise);
}, 3000);

 

 

const promise = new Promise((resolve, reject) => {
	setTimeout(() => {
    	console.log("μ•ˆλ…•");
        resolve("κ²°κ³Ό");
    }, 2000);
});

setTimeout(() => {
	console.log(promise);
}, 3000);

 

const promise = new Promise((resolve, reject) => {
    setTimeout(() => {
        console.log("μ•ˆλ…•");
        reject("μ‹€νŒ¨ν•œ 이유");
    }, 2000);
});

setTimeout(() => {
    console.log(promise);
}, 3000);

 

 

const promise = new Promise((resolve, reject) => {
    setTimeout(() => {
        const num = 10;

        if (typeof num === "number") {
            resolve(num + 10);
        } else {
            reject("num은 μˆ«μžκ°€ μ•„λ‹™λ‹ˆλ‹€.");
        }
    }, 2000);
});

promise.then((value) => {
    console.log(value); // 20
});

 

const promise = new Promise((resolve, reject) => {
    setTimeout(() => {
        const num = null;

        if (typeof num === "number") {
            resolve(num + 10);
        } else {
            reject("num은 μˆ«μžκ°€ μ•„λ‹™λ‹ˆλ‹€.");
        }
    }, 2000);
});

promise.catch((error) => {
    console.log(error); // num은 μˆ«μžκ°€ μ•„λ‹™λ‹ˆλ‹€.
});

 

Promise Chaining 

ν•˜λ‚˜μ˜ Promiseκ°€ λλ‚˜λ©΄ κ·Έ κ²°κ³Όλ₯Ό λ‹€μŒ Promise둜 λ„˜κΈ°λŠ” λ°©μ‹μœΌλ‘œ 연속적인 비동기 μž‘μ—…μ„ μ²˜λ¦¬ν•˜λŠ” νŒ¨ν„΄

new Promise((resolve, reject) => {
    setTimeout(() => resolve(10), 1000);
})
    .then(result => {
        console.log("Step 1:", result);
        return result * 2;
    })
    .then(result => {
        console.log("Step 2:", result);
        return result * 3;
    })
    .then(result => {
        console.log("Step 3:", result);
        return result - 5;
    })
    .then(finalResult => {
        console.log("Final result:", finalResult);
    });

 

μœ„μ˜ μ½”λ“œμ˜ 좜λ ₯ κ²°κ³ΌλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

(1초 ν›„)
Step 1: 10
Step 2: 20
Step 3: 60
Final result: 55

 

μ μš©ν•΄λ³΄κΈ°

new Promise((resolve, reject) => {
  console.log("Initial");

  resolve();
})
  .then(() => {
    throw new Error("Something failed");

    console.log("Do this");
  })
  .catch(() => {
    console.log("Do that");
  })
  .then(() => {
    console.log("Do this, whatever happened before");
  });

 

 

new Promiseκ°€ μ‹€ν–‰λ˜λ©΄μ„œ console.log("Initial"); κ°€ μ‹€ν–‰λΌμ„œ Initial이 좜λ ₯λ©λ‹ˆλ‹€.

 

resolve() κ°€ ν˜ΈμΆœλμœΌλ―€λ‘œ λ‹€μŒ .then() λΈ”λ‘μœΌλ‘œ μ΄λ™ν•˜κ³ , .then() λ‚΄λΆ€μ—μ„œ throw new Error("Something failed"); κ°€ μ‹€ν–‰λ©λ‹ˆλ‹€.

κ°•μ œλ‘œ μ—λŸ¬λ₯Ό λ°œμƒμ‹œν‚€κΈ° λ•Œλ¬Έμ— console.log("Do this");λŠ” μ‹€ν–‰λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

 

μ—λŸ¬κ°€ λ°œμƒν–ˆμœΌλ―€λ‘œ .catch() 둜 λ„˜μ–΄κ°€μ„œ console.log("Do that"); κ°€ μ‹€ν–‰λΌμ„œ Do that이 좜λ ₯λ©λ‹ˆλ‹€.

 

.then()으둜 λ„˜μ–΄κ°€μ„œ console.log("Do this, whatever happened before"); κ°€ μ‹€ν–‰λΌμ„œ Do this, whatever happened before이 좜λ ₯λ©λ‹ˆλ‹€.

 

좜λ ₯ κ²°κ³Ό

Initial
Do that
Do this, whatever happened before