zoaseo
To Infinity And Beyond
zoaseo
전체 방문자
오늘
어제
  • 분류 전체보기 (763)
    • 개발이 좋아서 (381)
      • SAP가 좋아서 (3)
      • Java가 좋아서 (42)
      • Spring이 좋아서 (50)
      • JPA가 좋아서 (0)
      • QueryDSL이 좋아서 (26)
      • Docker가 좋아서 (7)
      • Redis가 좋아서 (7)
      • AWS가 좋아서 (5)
      • CI/CD가 좋아서 (6)
      • Troubleshooting이 좋아서 (4)
      • Kotlin이 좋아서 (7)
      • SQL이 좋아서 (6)
      • HTTP가 좋아서 (21)
      • JavaScript가 좋아서 (30)
      • TypeScript가 좋아서 (6)
      • Vue가 좋아서 (21)
      • Flutter가 좋아서 (61)
      • React가 좋아서 (20)
      • Redux(React)가 좋아서 (2)
      • Angular가 좋아서 (22)
      • HTML이 좋아서 (9)
      • CSS가 좋아서 (15)
      • PHP가 좋아서 (9)
      • Illustrator가 좋아서 (2)
    • 노력이 좋아서 (169)
    • 결과물이 좋아서 (14)
    • 코딩연습이 좋아서 (168)
      • 이론이 좋아서 (62)
      • SQL이 좋아서 (90)
    • 유용한 사이트가 좋아서 (28)
    • Github (2)

인기 글

티스토리

hELLO · Designed By 정상우.
zoaseo

To Infinity And Beyond

노력이 좋아서

<step60>'js_비동기처리'

2022. 6. 15. 11:41

1) synchronous.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // function work(){
        //     const start = Date.now();
        //     for(let i=0; i<1000000000; i++){

        //     }
        //     const end = Date.now();
        //     console.log(end-start+'ms');
        // }
        function work(callback){
            setTimeout(()=>{
                const start = Date.now();
                for(let i=0; i<1000000000; i++){

                }
                const end = Date.now();
                console.log(end-start+'ms');
                callback();
            },0)
        }
        console.log('work호출하기');
        work(()=>{
            console.log('작업이 끝났어요');
        });
        console.log('다음 작업');
    </script>
</body>
</html>

2) 

myScript.js

function printHi(){
    console.log('안녕!!!');
}

 

callback.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // function loadScript(src){
        //     //script태그를 만들고 페이지에 script태그를 추가합니다.
        //     //태그가 페이지에 추가되면 src에 있는 스크립트를 로딩하고 실행합니다.
        //     let script = document.createElement('script');
        //     script.src = src;
        //     document.head.append(script);
        // }
        // loadScript("myScript.js");
        // printHi();
        function loadScript(src, callback){
            let script = document.createElement('script');
            script.src = src;
            script.onload = () => callback();
            document.head.append(script);
        }
        loadScript("myScript.js", function(){
            printHi();
        });
    </script>
</body>
</html>

3) callback2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        console.log('1');
        setTimeout(()=>{
            console.log('2');
        },2000)
        console.log('3');
        //콘솔창 : 132
        function printConsole(){
            console.log("자바스크립트 콜백!!!!!");
        }
        //synchronous 콜백 실습
        function printImmediately(print){
            print();
        }
        printImmediately(printConsole);
        //asynchronous 콜백 실습
        function printWithDelay(print,timeout){
            setTimeout(print,timeout);
        }
        printWithDelay(printConsole, 2000);
    </script>
</body>
</html>

4) promise.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //1. 프로미스 프로듀서
        const promise = new Promise((resolve, reject)=>{
            console.log('수행중......');
            setTimeout(()=>{
                resolve("결과");
            },2000)
        });
        console.log(promise);
        //2. 프로미스 컨슈머 then, catch, finally
        // promise.then(
            //결과를 다룹니다.
            // function(a){
            //     console.log('resolve실행');
            //     console.log(a);
            // },
            // a=>console.log(a),
            //에러를 다룹니다.
            // function(e){
            //     console.log('reject실행');
            //     console.log(e);
            // }
        //     e=>{
        //         console.log(e);
        //         console.log('reject실행');
        //     } 
        // )
        //성공한 결과는 then이 처리 실패하면 catch가 처리
        promise.then(value=>{
            console.log(value);
        })
        .catch(error=>{
            console.log(error);
        }).finally(()=>{
            console.log('끝났어요');
        })
    </script>
</body>
</html>

'노력이 좋아서' 카테고리의 다른 글

<step61>'js_canvas'  (0) 2022.06.16
<step60>'js_fetch_shoppinglist'  (0) 2022.06.15
<step59>'js_JSON'  (0) 2022.06.14
<step59>'js_복습(2)'  (0) 2022.06.14
<step58>'js_복습(1)'  (0) 2022.06.13

    티스토리툴바