ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • async await Ex1
    Search: Html Css JavaScript Html Css JavaScript 2022. 12. 24. 20:23

     

    async await 개요

    Promise

    Async Await Ex

     

     

    예제

    function resolveAfter2Seconds() {
      console.log('Promise start');
      return new Promise(resolve => {
        setTimeout(() => {
          resolve('resolved');
        }, 2000);
        console.log('Promise end');
      });
    }
    
    async function asyncCall() {
      console.log('async start');
      const result = await resolveAfter2Seconds();
      console.log('rst:' + result);
      // expected output: "resolved"
      console.log('async end');
    }
    
    console.log('프로그램 시작');
    asyncCall();
    setTimeout(() => {
        console.log('프로그램 setTimeout end');
        }, 2000);
    console.log('프로그램 종료');

     

    결과

    > "프로그램 시작"
    > "async start"
    > "Promise start"
    > "Promise end"
    > "프로그램 종료"
    > "rst:resolved"
    > "async end"
    > "프로그램 setTimeout end"

     

    대기 시간이 필요 한 것은 비동기로 등록해두어서,
    전체 처리에 지연시간 없이 절차를 진행할 수 있게 되었다.

     

    설명

    setTimeout은 비동기로 특정 시간 후 지정된 함수를 호출한다.

    Promise는 비동기 처리를 취합하기 위해 사용.

    async가 지정된 범위는 처리 시간이 외부와 비동기가 될 수 있으므로 언제 처리가 완료 될지 미지수가 된다. async외부는 결과 대기 없이 바로 다음 처리로 넘어갈 수 있다.

    await가 지정된 범위는 처리가 완료될 때 까지 진행을 기다린다.

     

    'Html Css JavaScript' 카테고리의 다른 글

    Css, Style 적용 방식 (내부, 외부, 직결, external, internal, inline)  (0) 2023.01.18
    javascript window, document  (0) 2023.01.03
    Promise Ex2  (0) 2022.12.30
    Promise 개요  (0) 2022.12.25
    Promise Ex1  (0) 2022.12.24
    async await 개요  (0) 2022.12.24
    Html Label  (0) 2022.12.01
    Javascript 중괄호({}, Braces)  (0) 2022.11.25

    댓글