0

My code is something like this:

for(let query of array1){ 
   request.get(query, function(err, res, body){
     var array2 = body
     for (let query2 of array2){
        request.get(query, function(err, res, body){
            var variable1 = body
            updateDB(query, query2 , variable1)
         });
     }
   });
}

suppose Array1 is [1,2,3,4]

with first loop array2 will [a,b,c,d](and second loop will give [e,f,g,h] and so on).

Now my updateDB just updates for first value of array2[in above case just for a and e] in each loop. where as i want it to loop through all the elements of array2(like b, c, d) before it moves to second set of array2(i.e [e,f,g,h]).

I know this is happening because of async nature of js. But how can i fix it? How can I make outer loop to wait for inner loop to finish?

Hope so I am clear enough. Thanks In advance!!

2
  • This is less an async problem and more of a classic closure problem. Look up "javascript closure in for loop" and why it's generally discouraged from doing so. Commented Apr 25, 2017 at 9:37
  • I know, its not a proper way of writing the js, but we are doing some db changes due to which i had to write one. This is just for one time use(hopefully) and its not going to be used in production. Commented Apr 25, 2017 at 12:14

3 Answers 3

1

If you want to first iterate the loop then you can use async.EachSeries

So it will iterate & you will get one by one value & you can use callback, so it will work as synchronously.

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

3 Comments

i started using async.EachSeries before you answered and it served my purpose. Marking it as answer so if anyone is in similar situation can find this.
@arjun kori If you are using async, then using callback it will be work as synchronous. Ex. If you want pass a value after doing some operation then you can use async.waterfall.
@bhaurao i did already, but since my reputation is less than 15 its not displayed publicly !!
1

You can use async.js module found on NPM to do just that. For your case the function that you are searching for is async.series. Here is the documentation for it: https://caolan.github.io/async/docs.html#series. async.series can be used to first run the first get request and then the second one but that does not mean that it will wait for the first loop (the upper for) to finish an itteration to go to the next one. For that you might want to nest two async loops.

1 Comment

yeah, i was already looking into the async, but .series or .waterfall, both were not easing the pain. I am looking into .foreach, have a feeling foreach might work. Will update here!!
0

You can use either the async module which is my favorite since you understand very easily how it works from the function name, but you can also use promises which are native for Javascript, and this is a great plus, since many beginners are using promises even without knowing how a callback works! So promises are becoming a standard.

If you want to learn more about those concepts : you can check this tutorial for async or this one for promises

1 Comment

due to some limitation of nature of my code execution env, i cant use promise. Its a non production code, just gonna get used once in full moon. My production code uses promises.

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.