1

I've build my first Node application. It receives a list of offers and then I have to receive prices for each item on that offer.

Now the issue is I'm using for to go through the offers object like so

for(var k = 0; k < offers_object.length; k++){
   offer = offers_object[k];
   logger.info('Time Loaded is '+Math.floor(new Date() / 1000));
   ....
   some stuff...
   ....
   logger.info('Time request is '+Math.floor(new Date() / 1000));
   ....
   send request
   ....
   some more stuff after the requst
}

Now the problem is that whatever needs to be done inside the for loop seems to not be running synchronous. I'm using an API to request item prices which limits me by 8 requests per second.

And what I'm getting from the timers is:

info: Time Loaded is 1448409307
info: Time Loaded is 1448409307
info: Time Loaded is 1448409307
info: Time Loaded is 1448409307
info: Time Loaded is 1448409307
info: Time Loaded is 1448409307
info: Time Loaded is 1448409307
info: Time request is 1448409308
info: Time request is 1448409308
info: Time request is 1448409308
info: Time request is 1448409308
info: Time request is 1448409308
info: Time request is 1448409308
info: Time request is 1448409309
9
  • 7
    the ...some stuff...'s are important. Can you fill them in? Commented Nov 24, 2015 at 23:59
  • Just clarifying the item prices you are requesting from the API are being stored in the offers_object correct? Commented Nov 25, 2015 at 0:01
  • Hey, almost certain that the Math.floor() and division by 1000 will knock off the difference. Try printing the actual time, down to ms? Commented Nov 25, 2015 at 0:01
  • Umm...it's pretty simple. logger.info('Time request is '+Math.floor(new Date() / 1000)); is in a callback which gets executed later. The for loop is forced to complete. So the callbacks can never run until the loop finishes. Commented Nov 25, 2015 at 0:02
  • i've pasted it here EDIT: updated -removed comments section codetidy.com/7382 Commented Nov 25, 2015 at 0:05

1 Answer 1

2

So, basically, you need to query an API only 8 times per second. To achieve this, you can loop on your items and use setTimeout to make a request each 1/8th of second :

var delay = 1000 / 8;
objects.forEach(function(val, index) {
    setTimeout(function() {
        doSomething(index, val);
    }, index * delay);
});

Staying asynchronous is very important for performance and reactivity. It should not prevent you from doing anything.

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

2 Comments

You sir are genius!! Just one more thing, why should i pass the index to the function if i'm passing the whole object to it ?
You don't have to, I'm just illustrating that you can.

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.