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

75
Visualizações
BestBuy API won't query in a loop

I am very new to Javascript and I am trying to utilize BestBuy's api to grab data on a specific sku number every 3 seconds.

The call to the api works as expected on its own but when I move the call inside a while loop, I do not get anything and the console.log in the function is not hit.

Not sure where to go from here and I am starting wonder if this api call can only be called by itself.

Would appreciate any help.

var bby = require('bestbuy')('<Actual BestBuy Api key in original code>');

var isSoldOut = true;

while(isSoldOut)
{
  console.log("HERE");
  bby.products(6465789,{show:'sku,name,salePrice,onlineAvailability,inStorePickup,onlineAvailabilityUpdateDate'}).then(function(data){
    console.log(data);

    if (data['onlineAvailability'] == false)
    {
    isSoldOut = false;
    console.log(isSoldOut);
    }
  });
  
  wait(3000);
}

function wait(ms)
{
    var d = new Date();
    var d2 = null;
    do { d2 = new Date(); }
    while(d2-d < ms);
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your while loops are constantly blocking the thread, so your callback function can't run.

Instead of the while loops, you can use an interval:

var bby = require('bestbuy')('<Actual BestBuy Api key in original code>');

function check() {
  console.log("HERE");
  bby.products(6465789,{show:'sku,name,salePrice,onlineAvailability,inStorePickup,onlineAvailabilityUpdateDate'}).then(function(data){
    console.log(data);

    if (data['onlineAvailability'] == false)
    {
      clearInterval(loop);
      console.log("In Stock");
    }
  });
}

const loop = setInterval(check, 3000);

Even cleaner may be using async/await to wait 3 seconds after receiving each response:

var bby = require('bestbuy')('<Actual BestBuy Api key in original code>');

const wait = require('util').promisify(setTimeout);

async function check() {
  console.log("HERE");

  const data = await bby.products(6465789,{show:'sku,name,salePrice,onlineAvailability,inStorePickup,onlineAvailabilityUpdateDate'});
  console.log(data);

  if (data['onlineAvailability'] == false)
  {
    console.log("In Stock");
    return;
  }

  await wait(3000);
}

check();
about 4 years ago · Juan Pablo Isaza Relatório

0

There are several points to improve your code:

  • Use setInterval to execute code in regular interval, instead of using spin lock in a while loop
  • Use async/await to improve readability
  • Instead of checking condition == false, simply check !condition
  • Instead of doing if(!condition){x = false}, simply do x = !condition
  • Use dot notation instead of bracket notation to access object property if the property name is not variable. This can take advantage of intellisense of IDE if the object shape is properly documented and reduce typo.

const t = setInterval(async () =>{
    const data = await bby.products(6465789, { show: 'sku,name,salePrice,onlineAvailability,inStorePickup,onlineAvailabilityUpdateDate' });
    console.log(data);
    isSoldOut = !data.onlineAvailability;
    console.log(isSoldOut);
    if(isSoldOut)
    {
        clearInterval(t);
    }
},3000);
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