I have an API that returns a list of replies (limit 5 replies per call) for a message board thread. What I am trying to do, is look for a specific reply uuid in the response. If not found, make another AXIOS GET call for the next 5 replies.
I want to continue this loop until the UUID is called or the AXIOS GET call comes back with no results.
Example API Request:
http://localhost:8080/api/v2/replies?type=thread&key=e96c7431-a001-4cf2-9998-4e177cde0ec3
Example API Reponse:
"status": "success",
"data": [
{
"uuid": "0a6bc471-b12e-45fc-bc4b-323914b99cfa",
"body": "This is a test 16.",
"created_at": "2017-07-16T23:44:21+00:00"
},
{
"uuid": "0a2d2061-0642-47eb-a0f2-ca6ce5e2ea03",
"body": "This is a test 15.",
"created_at": "2017-07-16T23:44:16+00:00"
},
{
"uuid": "32eaa855-18b1-487c-b1e7-52965d59196b",
"body": "This is a test 14.",
"created_at": "2017-07-16T23:44:12+00:00"
},
{
"uuid": "3476bc69-3078-4693-9681-08dcf46ca438",
"body": "This is a test 13.",
"created_at": "2017-07-16T23:43:26+00:00"
},
{
"uuid": "a3175007-4be0-47d3-87d0-ecead1b65e3a",
"body": "This is a test 12.",
"created_at": "2017-07-16T23:43:21+00:00"
}
],
"meta": {
"limit": 5,
"offset": 0,
"next_offset": 5,
"previous_offset": null,
"next_page": "http://localhost:8080/api/v2/replies?_limit=5&_offset=5",
"previous_page": null
}
The loop would call an AXIOS GET on the meta > next_page
url until either the uuid is found in the results or the meta > next_page
is null (meaning no more replies).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With