Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

71
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!