๐Ÿ“’JavaScript - Promise (then, catch, new Promise)

zooyahoยท2022๋…„ 8์›” 3์ผ
0

const data = fetch(`https://jsonplaceholder.typicode.com/posts`);
data
  .then((response) => {
    return response.json();
  })
  .catch((reason) => {
    console.log(reason);
  })
  .then((json) => {
    console.log(json);
  });

1. fetch()

fetch('api url')
: promise๋ฐ์ดํ„ฐํƒ€์ž…์˜ response object๋ฅผ ๋ฐ˜ํ™˜
๐Ÿ‘‰๐Ÿป ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ๋‘๊ฐœ์˜ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ

โ— then()

.then(function(result)=>{})
: promise๋ฐ์ดํ„ฐํƒ€์ž…์˜ ์ฒ˜๋ฆฌ๋œ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋ฐ›์Œ.

1. fetch().then()

fetch('api url').then(function(response)=>{})
: fetch๊ฐ€ ์„ฑ๊ณตํ•  ๊ฒฝ์šฐ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉฐ ๊ฒฐ๊ณผ๋ฅผ ์ธ์ž(result)๋กœ ๋ฐ›์Œ.
๐Ÿ‘‰๐Ÿป ์—ฌ๊ธฐ์„œ result๋Š” promise๋ฐ์ดํ„ฐํƒ€์ž…์˜ response object๋ฅผ ๊ฐ€๋ฆฌํ‚ด.

2. response.json().then()

response.json().then(function(data)=>{})
: response.json()์€ promise๋ฐ์ดํ„ฐํƒ€์ž…์„ ๊ฐ์ฒด๋ฐ์ดํ„ฐ ํ˜•ํƒœ๋กœ ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ธ์ž๋กœ ๋ฐ›์Œ(data)

โ— catch()

.catch(function(reason)=>{})
: fetch๊ฐ€ ์‹คํŒจํ•  ๊ฒฝ์šฐ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉฐ ์‹คํŒจ ์ด์œ ๋ฅผ ์ธ์ž(result)๋กœ ๋ฐ›์Œ.

โ— json()

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);});

โ— text()

response.text()
: ๊ฒฐ๊ณผ๋ฅผ ๋ฌธ์ž์—ด ํ˜•์‹์œผ๋กœ ์ œ๊ณต

2. new Promise

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!
}); 

โ— new Promise((resolve, reject)=>{})

: ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ธ์ž๋กœ Promise ์ƒ์„ฑ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•จ์ˆ˜๊ฐ’์ด ๋“ค์–ด์˜ด.
: promise๋ฐ์ดํ„ฐ ํƒ€์ž…์€ then(), catch()๋ฉ”์„œ๋“œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ.

1. ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ

๐Ÿ‘พ#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์ดˆ๋’ค ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅ ๋จ.

2. reject()

๐Ÿ‘พ#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()๋Š” ์‹คํ–‰๋˜์ง€ ์•Š์Œ~!

profile
์ฆ๊ฒ๊ฒŒ ๊ฐœ๋ฐœํ•˜์ž ์ฅฌ์•ผํ˜ธ๐Ÿ‘ป

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