🀟
Vanilla-JS
  • TRIPLE-PLUS Adout
  • Babel.JS
  • 01.Vanilla-JS
    • 01. Hello JavaScript
    • 02. λ³€μˆ˜
      • 02-1.μ§€ν‚€λ©΄ 쒋은 λ£°(더 쒋은 μŠ€νƒ€μΌμ„ μœ„ν•΄μ„œ)
      • 02-2.μˆ«μžν˜•κΏ€νŒ(μˆ˜μ‹ 쀄이기)
    • 03. μ—°μ‚°μž
    • 04. 쑰건문
    • 05. ν•¨μˆ˜
      • this의 의미 6개 총정리
      • event listener와 constructor
      • 05-1. return:λŒλ €μ£Όλ‹€
      • 05-2. ν˜• λ³€ν™˜ ν•¨μˆ˜
      • 05-3. Arrow function
    • 06. 객체
    • 07. λ°°μ—΄
      • 07-1. λ°°μ—΄ μ•ˆμ— λ°°μ—΄(2μ°¨ λ°°μ—΄)
      • 07-2. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(forEach)
      • 07-3. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(sort)
      • 07-4. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(map)
      • 07-5. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(indexOf)
      • 07-6. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(filter)
      • 07-7. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(splice, slice)
      • 07-8. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(shift, unshift, pop, push)
      • 07-9. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(concat, join)
      • 07-10. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(reduce)
      • 07-10. λ°°μ—΄λ‚΄μž₯ ν•¨μˆ˜(reduce)2
    • 08. 반볡문
    • 09. Class와 ν”„λ‘œν† νƒ€μž…
      • 09-1. ν”„λ‘œν† νƒ€μž…κ³Ό 클래슀 - μƒμ„±μž
      • 09-2. ν”„λ‘œν† νƒ€μž…κ³Ό 클래슀 - μƒμ„±μž μƒμ†ν•˜κΈ°
      • Class & Constructor & Method
      • super
      • Static Method
      • Class Getter, Setter
      • Information Hiding
      • Inheritance & Polymorphism
      • Overriding
      • Abstract Class
      • Interface
    • Math 정리
    • String 정리
    • Array정리
    • Date 정리
      • D-day κ΅¬ν•˜κΈ°
  • 02.Vanilla-JS
    • 01.μ‚Όν•­μ—°μ‚°μž
    • 02. Truthy and Falsy
    • 03. 단좕 평가 논리 계산법
    • 04. ν•¨μˆ˜ μ—…κ·Έλ ˆμ΄λ“œ (default parameter/arguments)
    • 05. 쑰건문 더 μŠ€λ§ˆνŠΈν•˜κ²Œ μ“°κΈ°
    • 06. 비ꡬ쑰화 ν• λ‹Ή (ꡬ쑰뢄해) 문법
    • 07. spread 와 rest
      • ν•¨μˆ˜ νŒŒλΌλ―Έν„°μ—μ„œμ˜ rest(params)
      • ν•¨μˆ˜ μΈμžμ—μ„œμ˜ spread
    • 08. μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ Scope 에 λŒ€ν•œ 이해
    • 09. Hoisting μ΄ν•΄ν•˜κΈ°
    • 10. Native DOM 선택
  • 03.비동기 처리
    • 01. fetch, then, catch[Promise] μ΄ν•΄ν•˜κΈ°
    • 02. 직접 λ§Œλ“€μ–΄λ³΄λŠ” Promise 객체
    • 03. async/await
    • 04. Promise all, Promise.race
  • REST API μ œλŒ€λ‘œ μ•Œκ³  μ‚¬μš©ν•˜κΈ°
    • mongodb Atlas
    • mongodb Atlas(DB)에 μ ‘κ·Όν•˜λ €λ©΄
    • URL 이름짓기 κ΄€μŠ΅
    • Database에 자료 μ €μž₯ν•˜λŠ” 법
    • κ²Œμ‹œλ¬Όλ§ˆλ‹€ 번호λ₯Ό 달아 μ €μž₯ν•˜κΈ°
    • κ²Œμ‹œλ¬Όλ§ˆλ‹€ 번호λ₯Ό 달아 μ €μž₯ν•˜κΈ°2
  • JQUERY + SCRIPT μœ μš©ν•œ μ½”λ“œ 정리
  • Practice(μ‹€μŠ΅)
    • μΉ΄μš΄ν„° λ§Œλ“€κΈ°
    • reduce
    • 데이터 μžλ£Œν˜• ν™œμš©ν•˜κΈ°1
    • 데이터 μžλ£Œν˜• ν™œμš©ν•˜κΈ°2
    • 데이터 κ°€μ§€κ³  놀기(μ‘μš©)1
    • 데이터 κ°€μ§€κ³  놀기(μ‘μš©)2
    • REST API
    • λ°°μ—΄ λ§Œλ“€κΈ°1
    • λ°°μ—΄ λ§Œλ“€κΈ°2
    • λ°°μ—΄ λ§Œλ“€κΈ°3
    • 비ꡬ쑰화 ν• λ‹Ή
    • rest + spread(μ‘μš©)1
    • rest + spread(μ‘μš©)2
    • rest + spread(μ‘μš©)3
    • 반볡문 λ‚΄μž₯ν•¨μˆ˜(μ‘μš©)1
    • ν•¨μˆ˜ κ°€μ§€κ³  놀기(μ‘μš©)1
    • μ„œλ²„μ™€μ˜ 톡신1
    • μ„œλ²„μ™€μ˜ 톡신2
    • new Promise(μ‘μš©)2
    • new Promise(μ‘μš©)3
    • μžλ°”μŠ€ν¬λ¦½νŠΈ 이벀트 μœ„μž„ 핡심 정리
Powered by GitBook
On this page
  • async/await
  • EX1)

Was this helpful?

  1. 03.비동기 처리

03. async/await

async/await

async/await 문법은 ES8에 ν•΄λ‹Ήν•˜λŠ” λ¬Έλ²•μœΌλ‘œμ„œ, Promise λ₯Ό λ”μš± μ‰½κ²Œ μ‚¬μš© ν•  수 있게 ν•΄μ€λ‹ˆλ‹€.

기본적인 μ‚¬μš©λ²•μ„ μ•Œμ•„λ΄…μ‹œλ‹€.

const sleep = (ms) => {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function process() {
    console.log('μ•ˆλ…•ν•˜μ„Έμš”!');
    await sleep(1000);
    console.log('λ°˜κ°‘μŠ΅λ‹ˆλ‹€.!');
}

process();
// κ²°κ³Ό)
μ•ˆλ…•ν•˜μ„Έμš”!
λ°˜κ°‘μŠ΅λ‹ˆλ‹€.!

async/await 문법을 μ‚¬μš©ν•  λ•Œμ—λŠ”, ν•¨μˆ˜λ₯Ό μ„ μ–Έ ν•  λ•Œ ν•¨μˆ˜μ˜ μ•žλΆ€λΆ„μ— async ν‚€μ›Œλ“œλ₯Ό λΆ™μ—¬μ£Όμ„Έμš”. 그리고 Promise 의 μ•žλΆ€λΆ„μ— await 을 λ„£μ–΄μ£Όλ©΄ ν•΄λ‹Ή ν”„λ‘œλ―ΈμŠ€κ°€ λλ‚ λ•ŒκΉŒμ§€ κΈ°λ‹€λ Έλ‹€κ°€ λ‹€μŒ μž‘μ—…μ„ μˆ˜ν–‰ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μœ„ μ½”λ“œμ—μ„œλŠ” sleep μ΄λΌλŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄μ„œ νŒŒλΌλ―Έν„°λ‘œ λ„£μ–΄μ€€ μ‹œκ°„ 만큼 κΈ°λ‹€λ¦¬λŠ” Promise λ₯Ό λ§Œλ“€κ³ , 이λ₯Ό process ν•¨μˆ˜μ—μ„œ μ‚¬μš©ν•΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

ν•¨μˆ˜μ—μ„œ async λ₯Ό μ‚¬μš©ν•˜λ©΄, ν•΄λ‹Ή ν•¨μˆ˜λŠ” κ²°κ³Όκ°’μœΌλ‘œ Promise λ₯Ό λ°˜ν™˜ν•˜κ²Œ λ©λ‹ˆλ‹€. λ”°λΌμ„œ λ‹€μŒκ³Ό 같이 μ½”λ“œλ₯Ό μž‘μ„± ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const sleep = (ms) => {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function process() {
    console.log('μ•ˆλ…•ν•˜μ„Έμš”!');
    await sleep(1000);
    console.log('λ°˜κ°‘μŠ΅λ‹ˆλ‹€.!');
}

process().then(() => {
    console.log('μž‘μ—… λλ‚¬μ–΄μš”!')
});
// κ²°κ³Ό)
μ•ˆλ…•ν•˜μ„Έμš”!
λ°˜κ°‘μŠ΅λ‹ˆλ‹€.!
μž‘μ—… λλ‚¬μ–΄μš”!

async ν•¨μˆ˜μ—μ„œ μ—λŸ¬λ₯Ό λ°œμƒ μ‹œν‚¬λ•Œμ—λŠ” throw λ₯Ό μ‚¬μš©ν•˜κ³ , μ—λŸ¬λ₯Ό μž‘μ•„λ‚Ό λ•Œμ—λŠ” try/catch 문을 μ‚¬μš©ν•©λ‹ˆλ‹€.

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function errorMake() {
    await sleep(3000);
    const error = new Error();
    throw error;
};

async function process() {
  try {
    await errorMake();
  } catch (e) {
    console.log('then error : ', e)   // μ—¬κΈ°μ„œ eλŠ” μœ„μ— throw error;λ₯Ό κ°€λ¦¬ν‚€κ²Œ λ©λ‹ˆλ‹€.!!
  }
}

process();
// κ²°κ³Ό)
then error : error

EX1)

new Promise 둜 μ •μ˜ ν•˜λŠ” μ½”λ“œ

function a(value) {
    return new Promise((resolve, reject) => {
        setTimeout(function() { // μ—¬κΈ°μ„œ setTimeout ν•œκ²ƒμ€ μ„œλ²„μ™€μ˜ ν†΅μ‹ μ—μ„œ μ‹œκ°„μ„ κ±Έλ¦°λ‹€λŠ” κ°€μ •ν•΄μ„œ 이미λ₯Ό setTimeoutν•¨μˆ˜λ₯Ό μ‚¬μš©ν•œκ²ƒμž…λ‹ˆλ‹€.
            resolve(value)
        },1000)
    })
}

function b(value) {
    return new Promise((resolve, reject) => {
        setTimeout(function() {  // μ—¬κΈ°μ„œ setTimeout ν•œκ²ƒμ€ μ„œλ²„μ™€μ˜ ν†΅μ‹ μ—μ„œ μ‹œκ°„μ„ κ±Έλ¦°λ‹€λŠ” κ°€μ •ν•΄μ„œ 이미λ₯Ό setTimeoutν•¨μˆ˜λ₯Ό μ‚¬μš©ν•œκ²ƒμž…λ‹ˆλ‹€.
            resolve(value)
        },1000)
    })
}

function c(value) {
    return new Promise((resolve, reject) => {
        setTimeout(function() {  // μ—¬κΈ°μ„œ setTimeout ν•œκ²ƒμ€ μ„œλ²„μ™€μ˜ ν†΅μ‹ μ—μ„œ μ‹œκ°„μ„ κ±Έλ¦°λ‹€λŠ” κ°€μ •ν•΄μ„œ 이미λ₯Ό setTimeoutν•¨μˆ˜λ₯Ό μ‚¬μš©ν•œκ²ƒμž…λ‹ˆλ‹€.
            resolve(value)
        },1000)
    })
}

function d(value) {
    return new Promise((resolve, reject) => {
        setTimeout(function() {  // μ—¬κΈ°μ„œ setTimeout ν•œκ²ƒμ€ μ„œλ²„μ™€μ˜ ν†΅μ‹ μ—μ„œ μ‹œκ°„μ„ κ±Έλ¦°λ‹€λŠ” κ°€μ •ν•΄μ„œ 이미λ₯Ό setTimeoutν•¨μˆ˜λ₯Ό μ‚¬μš©ν•œκ²ƒμž…λ‹ˆλ‹€.
            resolve(value)
        },1000)
    })
}

function test(value) {
    return new Promise((resolve, reject) => {
        if(false) { // μ •μƒμ μœΌλ‘œ μ„œλ²„μ™€ 톡신λͺ»ν•˜λ©΄ (μ—λŸ¬ λ‚˜λ©΄) μ‹€ν–‰λ©λ‹ˆλ‹€.
            reject('Error') //reject('μ›ν•˜λŠ” λ©”μ„Έμ§€ μž‘μ„±')
        }
        setTimeout(() => {  // μ—¬κΈ°μ„œ setTimeout ν•œκ²ƒμ€ μ„œλ²„μ™€μ˜ ν†΅μ‹ μ—μ„œ μ‹œκ°„μ„ κ±Έλ¦°λ‹€λŠ” κ°€μ •ν•΄μ„œ 이미λ₯Ό setTimeoutν•¨μˆ˜λ₯Ό μ‚¬μš©ν•œκ²ƒμž…λ‹ˆλ‹€.
            resolve(value) 
        }, 1000);
    })
}

then catch μ‹€ν–‰ν•˜λŠ” μ½”λ“œ

// then catch μ‹€ν–‰ν•˜λŠ” μ½”λ“œ

test('A').then((res) => {console.log(res)})
.then(() => b("B").then((res) => {console.log(res)}))
.then(() => c("C").then((res) => {console.log(res)}))
.then(() => d("D").then((res) => {console.log(res)}))
.catch((error) => {
    console.log(error)
    alert(error)
})
.finally(() => {
    console.log('done!!')
    alert('done!!')
})

ES8버전 async await μ‹€ν–‰ν•˜λŠ” μ½”λ“œ


// ES8버전 async await μ‹€ν–‰ν•˜λŠ” μ½”λ“œ

async function asyncFunc() {
    try {
        const a0 = await test('A!')
        console.log(a0)
        const a1 = await a('A')
        console.log(a1)
        const b1 = await b('B')
        console.log(b1)
        const c1 = await c('C')
        console.log(c1)
        const d1 = await d('D')
        console.log(d1)
    } catch (error) {
        alert(error)
        console.log(error)
    } finally {
        console.log('done!!')
    }
}

asyncFunc()
Previous02. 직접 λ§Œλ“€μ–΄λ³΄λŠ” Promise 객체Next04. Promise all, Promise.race

Last updated 5 years ago

Was this helpful?

840B
async.js.zip
archive
async.js μ˜ˆμ‹œ