I have recently came to know that it is hard to call asynchronous call in for loop. The solution was to use (function(x){})(x)
to maintain the variable x
for each asynchronous call. I see this kind of code often, but I can't figure out about how it maintains variable and about its grammar. I would be appreciated if you anyone can explain about this code snippet.
Asked
Active
Viewed 70 times
0

sean
- 1,644
- 1
- 15
- 14
-
4http://benalman.com/news/2010/11/immediately-invoked-function-expression/ – Arun P Johny Jul 01 '14 at 03:11
-
1it is used to create a local closure if you are talking about something like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures#Creating_closures_in_loops.3A_A_common_mistake – Arun P Johny Jul 01 '14 at 03:13
-
@ArunPJohny There's a dup question but couldn't find. – Bhojendra Rauniyar Jul 01 '14 at 03:15
-
@C-link: see on the "linked"? – zerkms Jul 01 '14 at 03:16
-
I marked [this](http://stackoverflow.com/questions/8228281/what-is-this-iife-construct-in-javascript) as a duplicate.. but then when I read the question again found that it is about closure usage... so even though the principle is the same I'm not sure whether it will answer the question – Arun P Johny Jul 01 '14 at 03:18
-
@ArunPJohny Your link is very helpful. I found the link on the page you mentioned. http://skilldrick.co.uk/2011/04/closures-explained-with-javascript/ I guess it's something like defining nested function and calling it right away, isn't it? – sean Jul 01 '14 at 03:19
-
See http://stackoverflow.com/questions/10896749/what-does-function-function-window-jquery-do/10896946#10896946 – jfriend00 Jul 01 '14 at 03:20
-
@jfriend00 this is about dup... – Bhojendra Rauniyar Jul 01 '14 at 03:21
-
@sean it is related to a concept called as [closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures) – Arun P Johny Jul 01 '14 at 03:21
-
Have a look at this too: http://stackoverflow.com/questions/18203897/a-self-invoking-anonymous-function-expression – Bhojendra Rauniyar Jul 01 '14 at 03:25