๐Ÿ‘ async await ์ง๋ ฌ / ๋ณ‘๋ ฌ + fetch ์ ์šฉ (๋น„๋™๊ธฐ)

KHWยท2021๋…„ 7์›” 20์ผ
0

Javascript ์ง€์‹์Œ“๊ธฐ

๋ชฉ๋ก ๋ณด๊ธฐ
64/95

1. ๋น„๋™๊ธฐ ๊ฐ„๋‹จ ์˜ˆ์‹œ

Case1) ๋ณ‘๋ ฌ (๊ฐ์ž ์ง„ํ–‰)

function a(){
  return new Promise(resolve=>{
    console.log('1');
    setTimeout(()=>{console.log('first setTimeout');},1000)
    resolve(1);
  })
}

function b(){
  return new Promise(resolve=>{
    console.log('2');
    setTimeout(()=>{console.log('second setTimeout');},1000);
    resolve(2);
  })
}

async function c(){
  const aa = await a();
  const bb = await b();
  return aa+bb;
}

c().then(console.log)

Case1) ๊ฒฐ๊ณผ

  1. a()ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ 1์ด ์ถœ๋ ฅ๋˜๊ณ 
  2. setTimeout์— ์˜ํ•ด 1์ดˆ๋’ค first setTimeout์ด ์ถœ๋ ฅ๋  ์˜ˆ์ •์ด๊ณ 
  3. resolve๋กœ ์ธํ•ด ๋ฐ”๋กœ aa๋กœ ๋ฐ˜ํ™˜์ด๋œ๋‹ค.
  4. b()ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ 2๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ 
  5. setTimeout์— ์˜ํ•ด 1์ดˆ๋’ค second setTimeout์ด ์ถœ๋ ฅ๋  ์˜ˆ์ •์ด๊ณ 
  6. resolve๋กœ ์ธํ•ด ๋ฐ”๋กœ bb๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ  aa+bb๊ฐ€ ๋ฐ˜ํ™˜๋œ 3์ด ์ถœ๋ ฅ๋œ๋‹ค.
  7. 1์ดˆ๋’ค ๊ฐ๊ฐ์˜ setTimeout์— ์˜ํ•ด first setTimeout๊ณผ second setTimeout์ด ์ถœ๋ ฅ๋œ๋‹ค.
1				//๋ฐ”๋กœ
2				//๋ฐ”๋กœ
3				//๋ฐ”๋กœ
first setTimeout		//1์ดˆ ๋’ค		
second setTimeout		//1์ดˆ ๋’ค

Case2) ์ง๋ ฌ(์„œ๋กœ ์˜ํ–ฅ๋ฐ›์•„ ์ง„ํ–‰)

function a(){
  return new Promise(resolve=>{
    console.log('1');
    setTimeout(()=>{console.log('first setTimeout'); resolve(1);},1000)
  })
}

function b(){
  return new Promise(resolve=>{
    console.log('2');
    setTimeout(()=>{console.log('second setTimeout'); resolve(2)},1000)
  })
}

async function c(){
  const aa = await a();
  const bb = await b();
  return aa+bb;
}

c().then(console.log)

Case2) ๊ฒฐ๊ณผ

  1. a()ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ 1์ด ์ถœ๋ ฅ๋œ๋‹ค.
  2. setTimeout์— ์˜ํ•ด first setTimeout์ด 1์ดˆ ๋’ค ์ถœ๋ ฅ๋  ์˜ˆ์ •์ธ๋ฐ resolve(1)์ด 1์ดˆ ๋’ค ์ด๋ฏ€๋กœ ๋‹ค์Œ ๋‚ด์šฉ์„ ์ง„ํ–‰ํ•˜๊ธฐ๊นŒ์ง€ 1์ดˆ๋™์•ˆ ๊ธฐ๋‹ค๋ฆฐ๋‹ค.
  3. 1์ดˆ ๋’ค first setTimeout์„ ์ถœ๋ ฅํ•˜๋ฉด์„œ b()ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ณ  2๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.
  4. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ setTimeout์— ์˜ํ•ด second setTimeout์ด ๊ทธ๋‹ค์Œ 1์ดˆ ๋’ค (์ฒ˜์Œ๊ธฐ์ค€ 2์ดˆ ๋’ค) ์ถœ๋ ฅ๋  ์˜ˆ์ •์ด๊ณ  resolve(2) ๋˜ํ•œ 1์ดˆ ๋’ค ์ด๋ฏ€๋กœ ๋‹ค์Œ ๋‚ด์šฉ์„ ์ง„ํ–‰ํ•˜์ง€์•Š๊ณ  ๊ธฐ๋‹ค๋ฆฐ๋‹ค.
  5. 1์ดˆ ๋’ค second setTimeout์„ ์ถœ๋ ฅํ•˜๊ณ  2๋ฅผ ๋ฆฌํ„ดํ•œ bb๋ฅผ ํ†ตํ•ด aa+bb๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ๋ฐ”๋กœ ๋‘ ํ•ฉ์ธ 3์„ ์ถœ๋ ฅํ•œ๋‹ค.
1			//๋ฐ”๋กœ
first setTimeout	//1์ดˆ ๋’ค
2			//1์ดˆ ๋’ค 
second setTimeout	//2์ดˆ ๋’ค
3			//2์ดˆ ๋’ค

2. ๋น„๋™๊ธฐ fetch ์ ์šฉ ์‹ค์ „

1) ์ˆœ์ฐจ์  ์ง„ํ–‰ (fetch)

async function a(){
  await b();
  console.log(2)
  await c();
  console.log(4)
}

function b(){
  return new Promise(resolve=>{
    fetch(`https://js-todo-list-9ca3a.df.r.appspot.com/api/users`,{
      method:"POST",
      headers:{"Content-Type":"application/json"},
      body:JSON.stringify({
        name : `$abc`
      })
    }).then(res=>res.json).then(data=>{
      console.log(1)
      resolve();
    }).catch(err=>console.log(err));

  })
}

function c(){
  return new Promise((resolve)=>{
    fetch(`https://js-todo-list-9ca3a.df.r.appspot.com/api/users`,{
    method:"POST",
    headers:{"Content-Type":"application/json"},
    body:JSON.stringify({
      name : `def`
    })
  }).then(res=>res.json).then(data=>{
    console.log(3)
      resolve();
    }).catch(err=>console.log(err));

  })
}



a();
  • ๊ฒฐ๊ณผ
Promise {<fulfilled>: undefined}
1
2
3
4

์ˆœ์ฐจ์ ์œผ๋กœ fetch ์™„๋ฃŒ ํ›„ ์ง„ํ–‰์„ ํ•œ๋‹ค.


2) ๋ณ‘๋ ฌ์  ์ง„ํ–‰ (fetch)

async function a(){
  await b();
  console.log(2)
  await c();
  console.log(4)
}

function b(){
  return new Promise(resolve=>{
    fetch(`https://js-todo-list-9ca3a.df.r.appspot.com/api/users`,{
      method:"POST",
      headers:{"Content-Type":"application/json"},
      body:JSON.stringify({
        name : `$abc`
      })
    }).then(res=>res.json).then(data=>{
      console.log(1)
    }).catch(err=>console.log(err));
    resolve();
  })
}

function c(){
  return new Promise((resolve)=>{
    fetch(`https://js-todo-list-9ca3a.df.r.appspot.com/api/users`,{
    method:"POST",
    headers:{"Content-Type":"application/json"},
    body:JSON.stringify({
      name : `def`
    })
  }).then(res=>res.json).then(data=>{
    console.log(3)
    }).catch(err=>console.log(err));
    resolve();
  })
}



a();
  • ๊ฒฐ๊ณผ
2
4
Promise {<fulfilled>: undefined}
1
3

resolve๋ฅผ ํ†ตํ•ด 2,4๋Š” ๋จผ์ € ์ถœ๋ ฅ๋˜๊ณ  ์‹œ๊ฐ„์ด ์ง€๋‚œ ํ›„ (์งง์€์‹œ๊ฐ„)
1,3์ด ๊ฐ๊ฐ ์ถœ๋ ฅ๋œ๋‹ค.


์ •๋ฆฌ

์šฐ๋ฆฌ๋Š” setTimeout์— ๋ฆฌํ„ดํ•˜๋Š” resolve๋ฅผ ๋„ฃ๋ƒ ์•ˆ ๋„ฃ๋ƒ์— ๋”ฐ๋ผ
๊ฐ๊ฐ์˜ await์ด ๊ฐ™์€ ์‹œ๊ฐ„์— ๊ฐ™์ด ์‹คํ–‰๋˜์–ด ๊ฐ™์ด ์ถœ๋ ฅ๋  ์ˆ˜๋„ ์žˆ๊ณ  ๋‹ค๋ฅธ await์„ ๊ธฐ๋‹ค๋ฆฐ ํ›„์— ๋‹ค์Œ await์„ ์‹คํ–‰ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

async๋ฅผ ํ†ตํ•ด ๋ฆฌํ„ดํ•œ ๊ฐ’์€ Promise์ด๋ฏ€๋กœ then์„ ํ†ตํ•ด ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

profile
๋‚˜์˜ ํ•˜๋ฃจ๋ฅผ ๊ฐ€๋Šฅํ•œ ๊ธฐ์–ตํ•˜๊ณ  ์ฆ๊ธฐ๊ณ  ํ›„ํšŒํ•˜์ง€๋ง์ž

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