Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
While loop with 2 conditions not working as expected

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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

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);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda