How can I "while loop" an Axios GET call till a condition is met? How can I "while loop" an Axios GET call till a condition is met? vue.js vue.js

How can I "while loop" an Axios GET call till a condition is met?


What you should search for is not while loop, but it's called Recursion:

while:

var counter = 10;while(counter > 0) {    console.log(counter--);}

recursion:

var countdown = function(value) {    if (value > 0) {        console.log(value);        return countdown(value - 1);    } else {        return value;    }};countdown(10);

It means that the function keeps calling itself based on specific criteria on the output. This way you can create a function that handles the response and call itself again if the value doesn't suit you well (semicode):

function get() {    axios.get('url').then(function(response) {        if (response.does.not.fit.yours.needs) {            get();        } else {            // all done, ready to go!        }    });}get();

If you want it chained with promises then you should spend some time figuring it out by yourself, just returning a promises each time ;)


This is trivial if you you are precompiling with something that supports async/await. Below is just an example. In your case you would be checking for your guid or an empty response.

new Vue({  el:"#app",  methods:{    async getStuff(){      let count = 0;      while (count < 5){        let data = await axios.get("https://httpbin.org/get")        console.log(data)        count++      }    }  },  mounted(){    this.getStuff()  }})

Or, per your response to my comment,

new Vue({  el:"#app",  async created(){      let count = 0;      while (count < 5){        let data = await axios.get("https://httpbin.org/get")        // check here for your guid/empty response        console.log(data)        count++      }  }})

Working example (in latest Chrome at least).