본문 바로가기
Javascript & jQuqery

Javascript) Javascript & jQuery ajax & Promise 사용하기

by 유노파이 2022. 4. 26.

동기적 프로그램 실행이 필요할때..

function doTheThing() {
  return new Promise((resolve, reject) => {
    $.ajax({
      url: window.location.href,
      type: 'POST',
      data: {
        key: 'value',
      },
      success: function (data) {
        resolve(data) // ajax 이후 실행
      },
      error: function (error) {
        reject(error)
      },
    }) // and ajax 
  })
}

doTheThing()
  .then((data) => {
  	// ajax 이후 실행
    console.log(data)
    doSomethingElse()
  })
  .catch((error) => {
    console.log(error)
  })

 

출처 https://www.taniarascia.com/how-to-promisify-an-ajax-call/

댓글