Preserving variables inside async calls called in a loop.

f you want to run asynchronous functions inside a loop, but still want to keep the index or other variables after a callback gets executed you can wrap your code in an IIFE (immediately-invoked function expression).

var arr = ['Hello', 'World', 'Javascript', 'Async', ':)'];
for( var i = 0; i < arr.length; i++) {

   }, 500);

Published: 6/20/2013
Author: Dominic Bartl
More Information:
Tags: function async
comments powered by Disqus