Etc/Frontend

[JavaScript] ํ”„๋กœ๋ฏธ์Šค(Promise)๋ž€?

z.zzz 2023. 3. 23. 11:33

๐Ÿ“ข ํ”„๋กœ๋ฏธ์Šค(Promise)๋ž€ ์‹คํ–‰์€ ๋ฐ”๋กœ ํ•˜๋˜, ๊ฒฐ๊ด๊ฐ’์€ ๋‚˜์ค‘์— ๋ฐ›๋Š” ๊ฐ์ฒด๋กœ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉ๋œ๋‹ค. ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐฐ์šฐ๊ธฐ ์ „์— ๋จผ์ € ๋™๊ธฐ, ๋น„๋™๊ธฐ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž

 

Synchronous์™€ Asynchronous

console.log(1)
console.log(2)
console.log(3)
console.log(4)	// 1-> 2 -> 3 -> 4

- ์•ž ๋ช…๋ น์ด ๋๋‚˜์•ผ ๊ทธ ๋‹ค์Œ ๋ช…๋ น์ด ์‹คํ–‰๋˜๋Š” ๊ฑธ "synchronousํ•˜๋‹ค"๊ณ  ํ•œ๋‹ค.

- ์–ด๋–ค ์ˆœ์„œ๋กœ ์‹คํ–‰๋˜๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์‰ฝ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์œผ๋‚˜, ๋ช…๋ น ์‹คํ–‰์— ์‹œ๊ฐ„์ด ๋” ๊ฑธ๋ฆฐ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

 

console.log(1)
console.log(2)
setTimeout(function() { console.log(3) }, 5000);
console.log(4)  // 1 -> 2 -> 4 -> 3

- ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉด  setTimeout ํ•จ์ˆ˜์˜ ์˜ํ–ฅ์œผ๋กœ 5์ดˆ ๋™์•ˆ console.log(4)์˜ ์‹คํ–‰์ด ์ง€์—ฐ๋˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, 4๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋‚˜์„œ setTimeout์ด ์‹คํ–‰๋œ๋‹ค.

- main์ด ๋˜๋Š” ์‹คํ–‰์ˆœ์„œ์™€๋Š” ๋ณ„๊ฐœ๋กœ, ์ž๊ธฐ๋งŒ์˜ ์‹œ๊ฐ„ํ‘œ์— ๋”ฐ๋ผ ๋…๋ฆฝ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ๊ฑธ "asynchronousํ•˜๋‹ค"๊ณ  ํ•œ๋‹ค.

- ์—ฌ๋Ÿฌ ๋ช…๋ น์ด ๋น ๋ฅด๊ฒŒ ์‹คํ–‰๋œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์œผ๋‚˜, ์ฝ”๋“œ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ํŒŒ์•…ํ•˜๊ธด ๋‹ค์†Œ ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

 

Synchronous์™€ Asynchronous์˜ ์ฐจ์ด๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : Introduction to Synchronous and Asynchronous Processing

 

์–ธ์ œ ๋น„๋™๊ธฐ์  ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š”๊ฐ€?

1. ์–ด๋–ค ๋ช…๋ น์˜ ์‹คํ–‰์ด ์–ธ์ œ ๋๋‚ ์ง€ ์˜ˆ์ธกํ•˜๊ธฐ ์–ด๋ ค์šธ ๋•Œ

2. ์ฃผ๊ฐ€ ๋˜๋Š” ์ž‘์—…์ด ์•„๋‹ ๋•Œ  ์˜ˆ) ํ†ต์‹ (๋‹ค๋ฅธ ์ž‘์—…์„ ํ•˜๋‹ค๊ฐ€ ํ†ต์‹ ์ด ๋๋‚˜๋ฉด ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒŒ ํšจ์œจ์ )

 

 

๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์˜ˆ์‹œ

javascript fetch api : ๋น„๋™๊ธฐ์  ํ†ต์‹  ๋ฐฉ๋ฒ•

console.log(1)
fetch('https://jsonplaceholder.typicode.com/posts')
  .then(function(response) {
    return response.json();	// json type์˜ ์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ js typye์œผ๋กœ converting
  })
  .then(function(myJson) {
    console.log(myJson);
  });
console.log(2)	// 1 -> 2 -> response.json

์‹คํ–‰ ์ˆœ์„œ

1. console.log(1), console.log(2)๊ฐ€ ๋จผ์ € ์‹คํ–‰๋œ๋‹ค.

2. ํ†ต์‹ ์ด ๋๋‚˜์„œ json data type์„ javascript data type์œผ๋กœ ๋ฐ”๊พธ๋Š” ์ž‘์—…์ด ๋๋‚œ๋‹ค.

3. 2์˜ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜์–ด console.log(myJson)์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

์‹คํ–‰ ์›๋ฆฌ - ํ”„๋กœ๋ฏธ์Šค(Promise)๋ž€?

Promise๋Š” ๋น„๋™๊ธฐ ์ž‘์—…์˜ ์ตœ์ข… ์™„๋ฃŒ ๋˜๋Š” ์‹คํŒจ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๋‹ค.

Promise ๊ฐ์ฒด๋Š” ๋‘ ๊ฐœ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋‘ ๋ฉ”์†Œ๋“œ ๋ชจ๋‘ ์ธ์ž๋กœ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š”๋‹ค.

- then() : fetch๋ฅผ ํ†ตํ•ด ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์„ฑ๊ณตํ–ˆ์„ ๋•Œ, then์œผ๋กœ ์ „๋‹ฌ๋œ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

            fetch ํ•จ์ˆ˜์˜ return ๊ฐ’์ธ Promise data๋ฅผ then์œผ๋กœ ๋ฐ›์œผ๋ฉด response ๊ฐ์ฒด๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค.

            response ๊ฐ์ฒด๋Š” http ํ—ค๋”, ์ƒํƒœ ์ฝ”๋“œ, ์‘๋‹ต body ๋“ฑ์˜ ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ์‘๋‹ตํ•˜๋Š” ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

- catch() : fetch๋ฅผ ํ†ตํ•ด ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์‹คํŒจํ–ˆ์„ ๋•Œ, catch๋กœ ์ „๋‹ฌ๋œ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. ํ˜ธ์ถœ ๊ฒฐ๊ณผ, ์—๋Ÿฌ ์›์ธ์„ ์ถœ๋ ฅํ•œ๋‹ค.

๏ผŠ fetch ํ•จ์ˆ˜์˜ return ๊ฐ’์€ Promise data type์ธ๋ฐ, ์–ด๋–ค ํ•จ์ˆ˜์˜ return ๊ฐ’์ด Promise๋ฉด ๊ทธ ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋™์ž‘ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค!

var fetched = fetch("https://jsonplaceholder.typicode.com/posts");
 console.log("fetched", fetched);  // Promise data type
fetched.then(function(result){
   console.log(result)  // Response Object
})
fetched.catch(function(reason){
  console.log('reason', reason);  // reason TypeError: fetch failed
})

์œ„ ์ฝ”๋“œ๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

// code simplification
fetch("https://jsonplaceholder.typicode.com/posts")
  .then(function(response){
    console.log("response", response.json());
  })
  .catch(function(reason){
  console.log('reason', reason);
  });

 

 

Nested Promise์™€ Promise Chaining

Nested Promise : then ์•ˆ์— then์„ ์“ด๋‹ค. (return ์—†์ด)

Promise Chaining : promise๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฐ”๊นฅ์—์„œ ๋˜ then์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

fetch("https://jsonplaceholder.typicode.com/posts")
  .then(function(response){
    response.json().then(function(data) {  // Nested promise
    console.log('data', data);
  })
  .catch(function(reason){
  console.log('reason', reason);
  })
  fetch("https://jsonplaceholder.typicode.com/posts")
  .then(function(response){
    return response.json();   // promise chaining
  })
  .catch(function(reason){
  console.log('reason', reason);
  })
  .then(function(data) {   // promise chaining
    console.log("data", data);
  });

 

 

 

๋”๋ณด๊ธฐ

ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์€ ์ƒํ™œ์ฝ”๋”ฉ๋‹˜์˜ JavaScript - promise ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•ด ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.