const data = fetch(`https://jsonplaceholder.typicode.com/posts`);
data
.then((response) => {
return response.json();
})
.catch((reason) => {
console.log(reason);
})
.then((json) => {
console.log(json);
});
fetch('api url')
: promise๋ฐ์ดํฐํ์
์ response object๋ฅผ ๋ฐํ
๐๐ป ์ฝ๋ฐฑํจ์๋ฅผ ์ธ์๋ก ๋ฐ๋ ๋๊ฐ์ ๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅํจ
.then(function(result)=>{})
: promise๋ฐ์ดํฐํ์
์ ์ฒ๋ฆฌ๋ ๊ฒฐ๊ณผ ๊ฐ์ ์ฝ๋ฐฑํจ์์ ์ธ์๋ก ๋ฐ์.
fetch('api url').then(function(response)=>{})
: fetch๊ฐ ์ฑ๊ณตํ ๊ฒฝ์ฐ ์ฝ๋ฐฑํจ์๊ฐ ํธ์ถ๋๋ฉฐ ๊ฒฐ๊ณผ๋ฅผ ์ธ์(result)๋ก ๋ฐ์.
๐๐ป ์ฌ๊ธฐ์ result๋ promise๋ฐ์ดํฐํ์
์ response object๋ฅผ ๊ฐ๋ฆฌํด.
response.json().then(function(data)=>{})
: response.json()์ promise๋ฐ์ดํฐํ์
์ ๊ฐ์ฒด๋ฐ์ดํฐ ํํ๋ก ์ฝ๋ฐฑํจ์ ์ธ์๋ก ๋ฐ์(data)
.catch(function(reason)=>{})
: fetch๊ฐ ์คํจํ ๊ฒฝ์ฐ ์ฝ๋ฐฑํจ์๊ฐ ํธ์ถ๋๋ฉฐ ์คํจ ์ด์ ๋ฅผ ์ธ์(result)๋ก ๋ฐ์.
response.json()
: ์ธ์๋ก ๋ฐ์ response๊ฐ ์น๋ธ๋ผ์ฐ์ ์๊ฒ jsonํํ๋๊ฑธ ์๋ ค promise๋ฐ์ดํฐ ํ์
์ผ๋ก ๋ฐ๊ฟ์ฃผ๊ณ , [[PromiseResult]]์ฌ๋ฟ์ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ฒดํ์
์ผ๋ก ์ ์ฅํจ. JSON์์ ์ด๋ฅผ ๊ตฌ๋ฌธ ๋ถ์ํ์ฌ ๊ฐ์ฒด๋ก ๋ณํ
๐ console.log(response.json());
๐๐ป [[PromiseResult]]์ฌ๋ฟ ์์ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ฒดํ์
(Array(100))์ผ๋ก ์ ์ฅ์ด ๋์ด์์!!
๐ return response.json();
๐๐ป returnํ ๊ฒฝ์ฐ ๊ฐ์ฒดํ์
๋ฐ์ดํฐ๊ฐ ๋ฐํ์ด ๋จ!! ์ฌ๊ธฐ์ ๊ฐ์ฒด๋ฐ์ดํฐ๋ฅผ then()์ ์ฝ๋ฐฑํจ์ ์ธ์๋ก ๋ค์ด์ด~!~!
: response.json().then((data)=>{console.log(data);});
response.text()
: ๊ฒฐ๊ณผ๋ฅผ ๋ฌธ์์ด ํ์์ผ๋ก ์ ๊ณต
var job1 = new Promise(function( resolve, reject ){
// ์คํํ ์ฝ๋๋ค
// Promise ์์ฑ ์ฑ๊ณต ์ ๋ฐํ๋ ์ฝ๋๋ฅผ resolve()๋ฉ์๋ ์ธ์์ ์์ฑ
resolve('resolved ok!');
// Promise ์์ฑ ์คํจ ์ ๋ฐํ๋ ์ฝ๋๋ฅผ reject()๋ฉ์๋ ์ธ์์ ์์ฑ
reject('rejected ok!');
});
job1.then((data)=>{
console.log(data); // resolved ok!
});
: ์ฝ๋ฐฑํจ์ ์ธ์๋ก Promise ์์ฑ ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ํจ์๊ฐ์ด ๋ค์ด์ด.
: promise๋ฐ์ดํฐ ํ์
์ then(), catch()๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅํจ.
๐พ#01 - promise 1๊ฐ ์ฌ์ฉ
function job1(){
return new Promise(function( resolve, reject ){
setTimeout(()=>{
resolve('resolved ok!');
}, 2000);
});
job1().then((data)=>{
console.log(data); // resolved ok!
});
๐๐ป 2๋ค์ resolved ok! ์ถ๋ ฅ
๐พ#02 - promise 2๊ฐ ์ฌ์ฉ
function job1(){
return new Promise(function( resolve, reject ){
setTimeout(()=>{
resolve('job1 ok!');
}, 2000);
});
}
function job2(){
return new Promise(function( resolve, reject ){
setTimeout(()=>{
resolve('job2 ok!');
}, 2000);
});
}
๐พ #02-1 : Nested ๋ฐฉ๋ฒ
job1().then((data)=>{
console.log(data); // job1 ok!
job2.then((data)=>{
console.log(data); // job2 ok!
});
});
๐พ #02-2 : chaining ๋ฐฉ๋ฒ
job1().then((data)=>{
console.log(data); // job1 ok!
return job2();
}).then((data)=>{
console.log(data); // job2 ok!
});
๐๐ป 02-1, 02-2 ๋ชจ๋ 2์ด๋ค ์ฐจ๋ก๋๋ก ์ถ๋ ฅ ๋จ.
๐พ#01 - reject()๋ง ์์ฑ
function job1(){
return new Promise(function( resolve, reject ){
setTimeout(()=>{
reject('job1 fail!'); // reject๋ง ์์ฑ
}, 2000);
});
}
job1()
.then((data)=>{console.log(data)})
.catch((reason)=>{console.log(reason)});
๐๐ป 2์ด๋ค์ job1 fail! ์ถ๋ ฅ
๐พ#02 - resolve(), reject() ๋ชจ๋ ์์ฑ
function job1(){
return new Promise(function( resolve, reject ){
setTimeout(()=>{
resolve('job1 ok!');
reject('job1 fail!');
}, 2000);
});
}
job1()
.then((data)=>{console.log(data)})
.catch((reason)=>{console.log(reason)});
๐๐ป 2์ด๋ค์ job1 ok! ์ถ๋ ฅ, resolve()๋ง ์คํ ๋จ!!
๐พ#03 - promise 2๊ฐ ์ฌ์ฉ, reject()๋ง ์์ฑ
function job1(){
return new Promise(function( resolve, reject ){
setTimeout(()=>{
reject('job1 fail!');
}, 2000);
});
}
function job2(){
return new Promise(function( resolve, reject ){
setTimeout(()=>{
resolve('job2 ok!');
}, 2000);
});
}
job1().then((data)=>{
console.log(data);
return job2();
})
.catch((reason)=>{
console.log(reason)
})
.then((data)=>{
console.log(data);
});
๐๐ป 2์ด๋ค์ job1 fail! ์ถ๋ ฅ๋๊ณ job2์ ๋ํ resolve()๋ ์คํ๋์ง ์์~!