while
is actually doing the right thing.
Since it's checking the condition continuously without any delay and getting true everytime, it's printing the info on console, if it does Million checks in a second and the conditions are met, the console will print the string.
So, we need to add a timeout/interval (called pollInterval) so that it checks only after desired time.
I have a different solution for your problem. You want to show progress/a filler text while the promise is going on.
const rp = require('request-promise')
// The base wrapper to stop polluting the environment
async function getHTML(url) {
// A sample filler function
function doSomethingElse() {
console.log(`Processing`);
}
// a filler function to use it later inside
function ensureData({
fillerFn,
pollInterval = 500
}) {
let result;
// grab the data and set it to result async
rp.get(url)
.then((html) => {
result = html
})
.catch((e) => console.log(e))
return new Promise(function(resolve, reject) {
// a self-executing function
(function waitForFoo() {
// if there is result, resolve it and break free
if (result) return resolve(result);
// otherwise run the filler function
fillerFn()
// execute itself again after the provided time
setTimeout(waitForFoo, pollInterval);
})();
});
}
// return or run it
ensureData({
fillerFn: doSomethingElse,
pollInterval: 500
})
.then((result) => {
console.log(result)
})
}
getHTML('http://httpbin.org/ip');