source

VueJ에서의 HTTP 요청 재귀 전송s

factcode 2022. 11. 26. 13:53
반응형

VueJ에서의 HTTP 요청 재귀 전송s

나는 그것을 하기 위해 많은 곳을 확인했다.그러나 이에 대한 해결책을 찾지 못했습니다.어떤 방법으로든 HTTP 투고 요구가 있다고 가정합니다.HTTP 포스트 요구를 재귀적으로 호출하려고 합니다.문제는 setInterval 함수를 사용하여 실행하려고 하면 연결 문제로 인해 큐에 보류 중인 요청이 있다는 것입니다.연결이 낮은 동안 큐에 누적됩니다.제가 원하는 것은 최근에 요청을 보내기 전에 요청을 보내지 않는 것입니다.어떻게 하면 좋을까요?

Promise를 반환하는 함수와 이 Promise를 해결하고 자신을 다시 호출하는 다른 함수를 만들 수 있습니다..then콜백:

new Vue({
  el: '#app',
  data: {
    fetchCounter: 0 // used here to prevent infinite requests 
  },
  methods: {
    myFetch: function() {
      return fetch('https://jsonplaceholder.typicode.com/users/1/')
    },
    loopFetch: function() {
      this.myFetch().then(res => {
      	console.log(res.status, ' ', this.fetchCounter++);
        if (this.fetchCounter < 10) { // repeat 10 times, then abort
          setTimeout(this.loopFetch, 1000); // sort of debounce
        } else {
          console.log('aborted')
        }
      })
    }
  },
  mounted() {
    this.loopFetch(); // first call of the loopFetch function 
  }
});
<script src="https://unpkg.com/vue/dist/vue.js"></script>

<div id="app"></div>

언급URL : https://stackoverflow.com/questions/44740817/sending-http-request-recursively-on-vuejs

반응형