I want my while loop to match 2 conditions in order to break the loop - res has to NOT be undefined, which is the case only when status code is 200, and obj.owner has to be equal to some value i set. It takes a couple of seconds for the owner on that page to update, so i need the while loop to run until the owner updates.
Here's the request function
const getOwner = () => {
const opts = {
url: 'https://example.com',
json: true,
simple: false,
resolveWithFullResponse: true
}
return request(opts)
.then(res => {
if (res.statusCode == 200) {
const obj = {
owner: res.body.owner
}
return obj
} else {
console.error(`Error: ${res.statusCode}`)
}
})
.catch(e => {
console.error(`Panic: ` + e.message)
})
}
And here's the while loop
let owner = await getOwner()
while (!owner && owner.owner != 'some value') {
owner = await getOwner()
}
console.log('Loop broken')
When I console.log(!owner, (owner.owner != 'some value')) after the first function call, before trying to enter into the loop, it returns false for the first condition, and true for the second.
When status code is other than 200, it doesnt return the object, so i need those 2 conditions together, as otherwise it crashes the program due to owner being undefined.
Now, when i execute this, it always skips the loop no matter what, im really lost at this point.
owner.owneris anything other than 'some value'. Could owner.owner always besome value?!ownerandowner.owner != 'some value'values, first one is false and second is true, and the loop is still being skipped..some value? change!=to===. I would console log owner immediately after you get it, right before the while loop line, and see what it is. Because if you are saying it is returns false for!ownerthan it is a falsy value like undefined