Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

This comment has been minimized.

Show comment Hide comment
@mainiak

mainiak Jun 28, 2013

@steve-taylor

This comment has been minimized.

Show comment Hide comment
@steve-taylor

steve-taylor Dec 30, 2013

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.

Owner

steve-taylor commented Dec 30, 2013

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

This comment has been minimized.

Show comment Hide comment
@eliasruizhz

eliasruizhz Mar 6, 2015

i love this <3

i love this <3

@Zr0ck

This comment has been minimized.

Show comment Hide comment
@Zr0ck

Zr0ck Apr 23, 2015

Very useful for me..! :-*

Zr0ck commented Apr 23, 2015

Very useful for me..! :-*

@beetleskin

This comment has been minimized.

Show comment Hide comment
@beetleskin

beetleskin Nov 18, 2015

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();
    }
}

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

This comment has been minimized.

Show comment Hide comment
@caroso1222

caroso1222 Jun 22, 2016

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

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

@shoaibmansoor

This comment has been minimized.

Show comment Hide comment
@shoaibmansoor

shoaibmansoor Nov 29, 2016

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

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

@lhcneto

This comment has been minimized.

Show comment Hide comment
@lhcneto

lhcneto Apr 28, 2017

Pure genius!

lhcneto commented Apr 28, 2017

Pure genius!

@ManiDOSS

This comment has been minimized.

Show comment Hide comment
@ManiDOSS

ManiDOSS Jan 19, 2018

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

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

@arpit17792

This comment has been minimized.

Show comment Hide comment
@arpit17792

arpit17792 Feb 13, 2018

Nice!

Nice!

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