๐ข ํ๋ก๋ฏธ์ค(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์ ์ฐจ์ด๋ฅผ ๊ทธ๋ฆผ์ผ๋ก ๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
์ธ์ ๋น๋๊ธฐ์ ์ฒ๋ฆฌ๋ฅผ ํ๋๊ฐ?
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 ๊ฐ์๋ฅผ ์ฐธ๊ณ ํด ์์ฑํ์ต๋๋ค.
'Etc > Frontend' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] async & await๋? (0) | 2023.03.23 |
---|---|
[JavaScript] ์ฝ๋ฐฑ ํจ์(callback function)๋? (0) | 2023.03.21 |
๋ด๋น๊ฒ์ด์ ๋ฐ์์ ๋งํฌ๋ฅผ ํด๋ฆญํ์ ๋ ํด๋ฆญ๋ ๋ด์ฉ์ ๋ณธ๋ฌธ์ ๋ํ๋ด๊ธฐ (0) | 2021.08.11 |
07.13 ๊ณต๋ถ์ผ์ง - Javascript 2์ฃผ์ฐจ (0) | 2021.07.14 |
07.13 (0) | 2021.07.14 |