async, await

column clash·2021년 8월 19일
0

//es8 2017
//async 는 함수앞에만 붙일 수 있다.
//async 를 function 앞에 붙이면 함수 실행 후에 promise 오브젝트가 남는다.
//promise 를 리턴한다.
// new Promise() 안해도.

 async function 더하기() {
        1 + 1;
      }

      더하기().then(function () {
        console.log("성공이에요.");
      });
      
      async function 더하기3() {
        return Promise.reject("실패임");
      }

      더하기3().catch(function (결과) {
        console.log(결과);
      });
sync function 더하기5() {
        var 프로미스 = new Promise(function (성공, 실패) {
          var 힘든연산 = 1 + 1;
          실패(100);
        });
        //프로미스가 해결될때까지 기다려.
        try {
          var 결과 = await 프로미스;
          console.log("더하기5", 결과);
        } catch {
          console.log("잘안됨");
        }
      }
       // 프로미스.then(function (결과) {
      //    console.log(결과);
      //  });
      // 를 대체하는 게   var 결과 = await 프로미스;
      더하기5();
(async function 버튼() {
        var 프로미스 = new Promise(function (성공, 실패) {
          document.querySelector("#btn").addEventListener("click", function () {
            성공("성공했어요");
          });
        });
        try {
          var 결과 = await 프로미스;
          console.log(결과);
        } catch {
          console.log("에러났어요.");
        }
      })();
profile
풀스택 개발 중...

0개의 댓글