//--------------------------------------

    //then을 이용해 순차적으로 덧셈

    function proc1(arg1){

        console.log('proc1 - 0'arg1);

        fnPromise(110)        

        .thenret => { //resolve

            console.log('then -1'ret); return ret;

        })        

         .thenret => {

             ret++;   console.log('then -2',ret);     return ret;     

        })

        .thenret => {

            ret++;   console.log('then -3',ret);     return ret;

        })

        .finallyret => {

            console.log('항상 호출- finally'ret);

        })

        .catchret => {

            console.log('에러 -catch'ret);//reject

        })

    }

 

    //----------------------------------

    // async , await 사용

    //비동기 함수를 여러번 호출, async

    async function proc2(arg1){

        console.log('proc2 - 0'arg1);

        var ret = null;

        try {

            ret = await fnPromise(110);

            console.log('proc2 - 2'ret);

 

            ret = await fnPromise(1ret);

            console.log('proc2 - 4'ret);

 

        } catch(error){

            console.log('proc2 - 에러'error);

        }

 

    }

 

    //--------------------------------------

    //기본 프라미스 함수(비동기)

    function fnPromise(idin1){

        return new Promise( (resolve1reject1=> {

            setTimeoutfunction() {

                console.log('fnPromise - 0'idin1);

                ifin1 !== false){

                    console.log('fnPromise - 1 - 성공'idin1);

                    resolve1(in1+1);

                }else{

                    console.log('fnPromise - 2 - 실패'idin1);

                    reject1(in1);

                }

            },1000);

        });

    }

 

    //------------------

    $(function(){        

        proc2();

    });

 

반응형
Posted by codens