0

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.

10
  • have you put a console log inside the loop to make sure it is skipping the loop? Right now this will run if 1) there is no owner and 2) owner.owner is anything other than 'some value'. Could owner.owner always be some value? Commented Jan 4, 2022 at 3:06
  • @about14sheep yes i have done that, and it is definitely skipping the loop fully. I've also logged the !owner and owner.owner != 'some value' values, first one is false and second is true, and the loop is still being skipped.. Commented Jan 4, 2022 at 3:18
  • if the first one comes up false it will skip it. the loop will only run if both are true. Have you made sure owner is not a promise? Commented Jan 4, 2022 at 3:23
  • @about14sheep so how can i make sure that owner is not undefined and is equal to the value? Commented Jan 4, 2022 at 3:25
  • so you want to make sure it is not undefined AND equal to 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 !owner than it is a falsy value like undefined Commented Jan 4, 2022 at 3:27

2 Answers 2

1

In my opinion, it's a bad idea to loop through a resource until you get the intended result you are seeking.

If you have control over the resource that you are calling, change it to return the result according to your expectation.

If you do not have control over the resource, in my opinion, the resource should not be return different results on different calls. If you get a different response on each call, then the resource is not idempotent. It's a bad design. Imagine, you would be looping n times, with that you may or may not get the results you are looking for.

I would suggest looking into implementation and fixing the root of the problem, rather than work around.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent points. This appears to be a bad looping design and the design should be fixed rather than try to put some lipstick on this bad implementation. Why would a function called rapidly in a loop ever return a different value? There's occasionally a reason for retry upon error, but even then not in a rapid fire loop like this and never in a forever while loop when there isn't even an error communicated back.
0

getOwner returns a Promise. You will need to await for it. And I don't think you really need the while loop:

let owner = await getOwner();
if(owner && owner.owner != 'some value') {
  /*do stuff with owner*/
}
console.log(owner);

1 Comment

Sorry i forgot to include that in my question, as i had to write a new function to not share anything sensitive from the original one. But yes i am awaiting it already and the result is still the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.