πŸ’»
Albert's Til
GitHub
  • 맀일맀일 μ‘°κΈˆμ”© μ„±μž₯ν•˜κΈ°
    • README
    • CS
      • Network
      • HTTP
        • NO-CACHE
      • 였λ₯˜ μ½”λ“œ
      • ORM 도ꡬ
      • Design Pattern
        • CQRS Pattern
          • Event Sourcing and CQRS pattern
        • Builder Pattern
    • DB
      • MySQL
        • Timeline
        • Pagination
        • Index
        • Database Performance Optimization Strategies
        • B+ tree
        • MySQL Connectors VS MySQL Shell(Scripting) VS MySQL Workbench
        • MySQL Storage Engine Architecture
      • Normalization & Denormalization
      • JPA
        • @Transactional
        • Why JPA?
        • About JPA
        • N+1 Issue
        • Index
        • ElementCollection&CollectionTable
        • orphanRemoval
        • CascadeType
        • Use Subselect
        • Dynamic Instance Creation
        • Paging
        • Order
        • Spefication
        • mappedBy
      • MongoDB
        • ObjectId
      • Why MySQL?
      • ACID properties of transactions
      • Between JPA and JDBC
      • Identifiers in Hibernate/JPA
    • Java
      • Jackson de/serialize
      • Collections.singletonList() vs List.of()
      • Manage dependencies in Gradle
      • Logging Level
      • Bean Validation
      • JVM Internals
        • Threads
          • Frame
        • Shared Between Threads
          • Classloader
            • Class Loader Hierarchy
            • Loading Linking Initialization
      • Java Collection Framework
      • Annotation
      • Generic
      • λ””λ―Έν„° 법칙
    • Spring
      • Caching
      • Spring Integration Overview
        • ThreadPollTaskExecutor
        • Messaging Bridge
        • Channel Adapter
        • Poller
        • Configuration and @EnableIntegration
        • Message Endpoints
        • Message Channels
      • HATEOAS
      • @Autowired vs Constructor Dependency Injection
      • Spring Security
        • JWT 토큰 μ‚¬μš©ν•œ 인가
        • OAuth 2 Login
        • OAuth 2 인증
        • 인가
        • 인증
        • PasswordEncoder
      • IoC Container
      • Filter,Interceptor,AOP,Argument Resolver
      • Spring Annotation
      • About Spring
    • Kafka
      • Error Channel
    • Infra
      • Scale Up || Scale Out
      • Docker
        • Dockerfile
        • Docker Hub Deploy
        • Command
      • Cloud μœ ν˜•
        • Infrastructure as a Service
        • Platform as a Service
        • Software as a Service
      • 무쀑단 배포
        • μ—”μ§„μ—‘μŠ€(Nginx)
      • μ½”λ“œ μžλ™ 배포
        • Technical
      • AWS EC2
        • PEM(Privacy Enhanced Mail) ν‚€
      • AWS RDS
      • AWS S3
    • CodeSquad
      • Spring Boot Project 1μ£Όμ°¨ 회고
      • Spring Boot Project 2μ£Όμ°¨ 회고
      • Spirng Boot Project 3μ£Όμ°¨ 회고
      • Spring Boot Project 4μ£Όμ°¨ 회고
    • Foody Moody ν”„λ‘œμ νŠΈ
      • Query Performance Comparison
      • HeartCount Asynchronous Issue
      • DeferredResult
      • ResponseBodyEmitter
      • SseEmitter (Spring)
      • Server-Sent Events (SSE)
      • 기술 μŠ€νƒ 적용 이유
      • NO-CACHE(HTTP)
      • Transactional
    • DDD
      • AggregateId
    • Test
      • RestAssured
    • Coding and Algorithmic Problems
      • 819. Most Common Word
      • 344. Reverse String
      • 125. Valid Palindrome
      • 937. Reorder Data in Log Files
    • Node
      • Async... Await...
      • Custom Transactional Decorator Challenger
    • Python
      • Python Basic Grammar
        • Comments and Input/Output
        • Variable
        • Data type
        • Operations and syntax
        • List,Tuple,Dictionary,Set
        • Function
        • Conditional statement
        • Loop
    • HTML
      • HTML Basic
      • HTML Basic Tags
      • HTML Form Tags
      • HTML Table Tags
    • CSS
      • CSS Basic
      • CSS Practice
Powered by GitBook
On this page
  • async function
  • async
  • await
  • μ˜ˆμ‹œ

Was this helpful?

  1. 맀일맀일 μ‘°κΈˆμ”© μ„±μž₯ν•˜κΈ°
  2. Node

Async... Await...

async function

  • μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” 기본적으둜 λ™κΈ°μ μœΌλ‘œ μ‹€ν–‰λ˜λŠ” μ–Έμ–΄λ‹€.

    • μ΄λŠ” μ½”λ“œκ°€ μž‘μ„±λœ μˆœμ„œλŒ€λ‘œ ν•œ 쀄씩 μ‹€ν–‰λ˜λ©°, 각 라인의 μ½”λ“œκ°€ μ™„λ£Œλ˜κΈ° μ „κΉŒμ§€λŠ” λ‹€μŒ 라인으둜 λ„˜μ–΄κ°€μ§€ μ•ŠλŠ”λ‹€λŠ” 것을 μ˜λ―Έν•œλ‹€.

    • μ΄λŸ¬ν•œ 동기적 μ‹€ν–‰ λͺ¨λΈμ€ μ½”λ“œμ˜ 흐름을 μ˜ˆμΈ‘ν•˜κΈ° μ‰½κ²Œ ν•˜μ£Όμ§€λ§Œ, 였래 κ±Έλ¦¬λŠ” μž‘μ—…(예: λ„€νŠΈμ›Œν¬ μš”μ²­)을 μ²˜λ¦¬ν•  λ•Œ λ¬Έμ œκ°€ λ°œμƒν•  수 μžˆλ‹€.

  • async와 await은 μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ 비동기 μž‘μ—…μ„ 더 νŽΈλ¦¬ν•˜κ²Œ 닀루기 μœ„ν•΄ μ‚¬μš©λ˜λŠ” ν‚€μ›Œλ“œλ‹€.

async

  • 이 ν‚€μ›Œλ“œλŠ” 비동기 ν•¨μˆ˜λ₯Ό μ„ μ–Έν•˜λŠ” 데 μ‚¬μš©λœλ‹€.

  • async ν•¨μˆ˜λŠ” Promise λ₯Ό λ°˜ν™˜ν•œλ‹€.

  • μ„œλ²„μ—μ„œ 데이터λ₯Ό κ°€μ Έμ˜€λŠ” λ“±μ˜ μ‹œκ°„μ΄ κ±Έλ¦¬λŠ” μž‘μ—…μ„ μˆ˜ν–‰ν•˜λŠ” ν•¨μˆ˜κ°€ μžˆμ„ λ•Œ asyncλ₯Ό μ„ μ–Έν•˜κ³  μ‚¬μš©ν•œλ‹€.

  • μžλ°”μŠ€ν¬λ¦½νŠΈμ—κ²Œ 이 ν•¨μˆ˜λ₯Ό λΉ„λ™κΈ°μ μœΌλ‘œ μ²˜λ¦¬ν•˜λΌκ³  μ•Œλ €μ€€λ‹€

async function fetchData() {
	// APIμ—μ„œ 데이터 κ°€μ Έμ˜€κΈ°
}

await

  • 이 ν‚€μ›Œλ“œλŠ” async ν•¨μˆ˜ λ‚΄μ—μ„œ Promise κ°€ 해결될 λ•ŒκΉŒμ§€ κΈ°λ‹€λ¦¬λŠ” 데 μ‚¬μš©λœλ‹€.

  • ν•¨μˆ˜μ˜ 싀행을 Promise κ°€ 처리될 λ•ŒκΉŒμ§€(μ΄ν–‰λ˜κ±°λ‚˜ 거뢀될 λ•ŒκΉŒμ§€) μΌμ‹œ μ€‘μ§€ν•œλ‹€.

  • await λŠ” 였직 async ν•¨μˆ˜ λ‚΄μ—μ„œλ§Œ μ‚¬μš©ν•  수 μžˆλ‹€.

async function fetchData() {
  let data = await fetch('https://api.example.com/data');
  let jsonData = await data.json();
  return jsonData;
}

μ˜ˆμ‹œ

await μ‚¬μš©ν•˜λŠ” 경우

function resolveAfter2Seconds() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve('resolved');
      console.log('promise');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result);
  // Expected output: 
  // > "calling" 
  // > "promise" 
  // > "resolved"
}

asyncCall();

await μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 경우

function resolveAfter2Seconds() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve('resolved');
      console.log('promise');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = resolveAfter2Seconds();
  console.log(result);
  // Expected output:
  // > "calling" 
  // > [object Promise] > 
  // "promise"`


asyncCall();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Last updated 1 year ago

Was this helpful?