I'm looking to make a function which polls a certain URL synchronously (i.e. send one request, wait for it to come back before sending another). However, I'm unsure of why this is not working.
const request = require('request')
let count = 0;
function test() {
setInterval(() => {
console.log(count)
request({uri: "https://google.com"}, (err, resp, body) => {
console.log(count)
count++;
})
}, 100);
}
I would wait it to print count
in sequential order (1 2 3 4 5 ...
) but it currently prints same numbers several times indicating that it's not the behaviour that I intended.
How do I get it to wait for the callback before doing another interval?