Skip to content

Instantly share code, notes, and snippets.

@steve-taylor
Last active December 4, 2022 00:51
Show Gist options
  • Save steve-taylor/5075717 to your computer and use it in GitHub Desktop.
Save steve-taylor/5075717 to your computer and use it in GitHub Desktop.
Synchronous for loop in JavaScript to ensure that the n+1th item isn't processed until the callback of the nth item's processor has been called.
/**
* Process an array of data synchronously.
*
* @param data An array of data.
* @param processData A function that processes an item of data.
* Signature: function(item, i, callback), where {@code item} is the i'th item,
* {@code i} is the loop index value and {@code calback} is the
* parameterless function to call on completion of processing an item.
*/
function doSynchronousLoop(data, processData, done) {
if (data.length > 0) {
var loop = function(data, i, processData, done) {
processData(data[i], i, function() {
if (++i < data.length) {
loop(data, i, processData, done);
} else {
done();
}
});
};
loop(data, 0, processData, done);
} else {
done();
}
}
@mainiak
Copy link

mainiak commented Jun 28, 2013

@steve-taylor
Copy link
Author

This was just a learning exercise for me and I posted it here for myself, but I made it public because there's no reason to hide it.

@eliasruizhz
Copy link

i love this <3

@Zr0ck
Copy link

Zr0ck commented Apr 23, 2015

Very useful for me..! :-*

@beetleskin
Copy link

You'll run easily into the maximum off call stacks. Workaround:

function doSynchronousLoop(data, processData, done) {
    if (data.length > 0) {
        var loop = function(data, i, processData, done) {
            processData(data[i], i, function() {
                if (++i < data.length) {
                    setTimeout(function(){
                                            loop(data, i, processData, done);
                                        }, 0);
                } else {
                    done();
                }
            });
        };
        loop(data, 0, processData, done);
    } else {
        done();
    }
}

@caroso1222
Copy link

Genius! worked like a charm. Can we have a little bit of an explanation about why this works? :)

@shoaibmansoor
Copy link

Nice work !!! Kind of a cool way to implement sync loops 👍

@lhcneto
Copy link

lhcneto commented Apr 28, 2017

Pure genius!

@maranmat
Copy link

Nice work!!!!!!!!!!!!! you are genius!

@arpit17792
Copy link

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment