Skip to main content
added 8 characters in body
Source Link
Randy
  • 312
  • 1
  • 9
    var regions = [
        [10, 10000, 100000],
        [12, 10000, 100000],
        [14, 10000, 100000],
        [18, 10000, 100000]
    ];


    var d = [];
    var readers = [];
    var callbacksRemaining = regions.length;

    regions.forEach(function(item, i) {
        // strip the function from your forEach loop for code control.
        readers[i] = new readBinaryVCF(tabix, vcf, nameThisProperly);

        var nameThisProperly = function(tabixReader, i) {

            // add var here
            // you're not in strict mode so this gets pushed to the global scope
            // in strict mode you get errors.
            var reader = readers[i];

            // you can use reversed psychology for this loop:
            var index = 0;
            for (var key in tabixReader.idxContent.namehash) {
                if (key == regions[i][0]) {
                    index = tabixReader.idxContent.namehash[key];
                }
            }
            // example:
            var index = tabixReader.idxContent.namehash[regions[i][0]];
            // obviously untested, but something like this should work.


            // using index after the for loop seems weird. I would rename this variable, if it is only used to
            // work with the last element of the loop. Make it a good one ;) Now its like you would
            // run this method every time the for loop is called, but it is not.
            reader.getRecords(index, regions[i][1], regions[i][2], checkForCallback);
        };

        // strip this method for readability
        var checkForCallback = function(data) {
            d[i] = data;
            callbacksRemaining--;

            if (callbacksRemaining <= 0) {
                processData(d);
            }
        };
        // naming your functions like this makes it easy to know what's happening.
    });
    var regions = [
        [10, 10000, 100000],
        [12, 10000, 100000],
        [14, 10000, 100000],
        [18, 10000, 100000]
    ];


    var d = [];
    var readers = [];
    var callbacksRemaining = regions.length;

    regions.forEach(function(item, i) {
        // strip the function from your forEach loop for code control.
        readers[i] = new readBinaryVCF(tabix, vcf, nameThisProperly);

        nameThisProperly = function(tabixReader, i) {

            // add var here
            // you're not in strict mode so this gets pushed to the global scope
            // in strict mode you get errors.
            var reader = readers[i];

            // you can use reversed psychology for this loop:
            var index = 0;
            for (var key in tabixReader.idxContent.namehash) {
                if (key == regions[i][0]) {
                    index = tabixReader.idxContent.namehash[key];
                }
            }
            // example:
            var index = tabixReader.idxContent.namehash[regions[i][0]];
            // obviously untested, but something like this should work.


            // using index after the for loop seems weird. I would rename this variable, if it is only used to
            // work with the last element of the loop. Make it a good one ;) Now its like you would
            // run this method every time the for loop is called, but it is not.
            reader.getRecords(index, regions[i][1], regions[i][2], checkForCallback);
        };

        // strip this method for readability
        checkForCallback = function(data) {
            d[i] = data;
            callbacksRemaining--;

            if (callbacksRemaining <= 0) {
                processData(d);
            }
        };
        // naming your functions like this makes it easy to know what's happening.
    });
    var regions = [
        [10, 10000, 100000],
        [12, 10000, 100000],
        [14, 10000, 100000],
        [18, 10000, 100000]
    ];


    var d = [];
    var readers = [];
    var callbacksRemaining = regions.length;

    regions.forEach(function(item, i) {
        // strip the function from your forEach loop for code control.
        readers[i] = new readBinaryVCF(tabix, vcf, nameThisProperly);

        var nameThisProperly = function(tabixReader) {

            // add var here
            // you're not in strict mode so this gets pushed to the global scope
            // in strict mode you get errors.
            var reader = readers[i];

            // you can use reversed psychology for this loop:
            var index = 0;
            for (var key in tabixReader.idxContent.namehash) {
                if (key == regions[i][0]) {
                    index = tabixReader.idxContent.namehash[key];
                }
            }
            // example:
            var index = tabixReader.idxContent.namehash[regions[i][0]];
            // obviously untested, but something like this should work.


            // using index after the for loop seems weird. I would rename this variable, if it is only used to
            // work with the last element of the loop. Make it a good one ;) Now its like you would
            // run this method every time the for loop is called, but it is not.
            reader.getRecords(index, regions[i][1], regions[i][2], checkForCallback);
        };

        // strip this method for readability
        var checkForCallback = function(data) {
            d[i] = data;
            callbacksRemaining--;

            if (callbacksRemaining <= 0) {
                processData(d);
            }
        };
        // naming your functions like this makes it easy to know what's happening.
    });
added 48 characters in body
Source Link
Randy
  • 312
  • 1
  • 9
    var regions = [
        [10, 10000, 100000],
        [12, 10000, 100000],
        [14, 10000, 100000],
        [18, 10000, 100000]
    ];


    var d = [];
    var readers = [];
    var callbacksRemaining = regions.length;

    regions.forEach(function(item, i) {
        // strip the function from your forEach loop for code control.
    // try not to nest loops if you don't need to.
    readers[i] = new readBinaryVCF(tabix, vcf, nameThisProperly);
});

function        nameThisProperly = function(tabixReader, i) {

            // add var here
            // you're not in strict mode so this gets pushed to the global scope
            // in strict mode you get errors.
            var reader = readers[i];

            // you can use reversed psychology for this loop:
            var index = 0;
            for (var key in tabixReader.idxContent.namehash) {
                if (key == regions[i][0]) {
                    index = tabixReader.idxContent.namehash[key];
                }
            }
            // example:
            var index = tabixReader.idxContent.namehash[regions[i][0]];
            // obviously untested, but something like this should work. 


    

         // using index after the for loop seems weird. I would rename this variable, if it is only used to
            // work with the last element of the loop. Make it a good one ;) Now its like you would
            // run this method every time the for loop is called, but it is not.
            reader.getRecords(index, regions[i][1], regions[i][2], checkForCallback);
        };

        // strip this method for readability
function        checkForCallback = function(data) {
            d[i] = data;
            callbacksRemaining--;

            if (callbacksRemaining <= 0) {
                processData(d);
            }
        };
        // naming your functions like this makes it easy to know what's happening.
    });
var regions = [
    [10, 10000, 100000],
    [12, 10000, 100000],
    [14, 10000, 100000],
    [18, 10000, 100000]
];


var d = [];
var readers = [];
var callbacksRemaining = regions.length;

regions.forEach(function(item, i) {
    // strip the function from your forEach loop for code control.
    // try not to nest loops if you don't need to.
    readers[i] = new readBinaryVCF(tabix, vcf, nameThisProperly);
});

function nameThisProperly(tabixReader) {

    // add var here
    // you're not in strict mode so this gets pushed to the global scope
    // in strict mode you get errors.
    var reader = readers[i];

    // you can use reversed psychology for this loop:
    var index = 0;
    for (var key in tabixReader.idxContent.namehash) {
        if (key == regions[i][0]) {
            index = tabixReader.idxContent.namehash[key];
        }
    }
    // example:
    var index = tabixReader.idxContent.namehash[regions[i][0]];
    // obviously untested, but something like this should work.
    

     // using index after the for loop seems weird. I would rename this variable, if it is only used to
    // work with the last element of the loop. Make it a good one ;) Now its like you would
    // run this method every time the for loop is called, but it is not.
    reader.getRecords(index, regions[i][1], regions[i][2], checkForCallback);
}

// strip this method for readability
function checkForCallback(data) {
    d[i] = data;
    callbacksRemaining--;

    if (callbacksRemaining <= 0) {
        processData(d);
    }
}
// naming your functions like this makes it easy to know what's happening.
    var regions = [
        [10, 10000, 100000],
        [12, 10000, 100000],
        [14, 10000, 100000],
        [18, 10000, 100000]
    ];


    var d = [];
    var readers = [];
    var callbacksRemaining = regions.length;

    regions.forEach(function(item, i) {
        // strip the function from your forEach loop for code control.
        readers[i] = new readBinaryVCF(tabix, vcf, nameThisProperly);

        nameThisProperly = function(tabixReader, i) {

            // add var here
            // you're not in strict mode so this gets pushed to the global scope
            // in strict mode you get errors.
            var reader = readers[i];

            // you can use reversed psychology for this loop:
            var index = 0;
            for (var key in tabixReader.idxContent.namehash) {
                if (key == regions[i][0]) {
                    index = tabixReader.idxContent.namehash[key];
                }
            }
            // example:
            var index = tabixReader.idxContent.namehash[regions[i][0]];
            // obviously untested, but something like this should work. 


            // using index after the for loop seems weird. I would rename this variable, if it is only used to
            // work with the last element of the loop. Make it a good one ;) Now its like you would
            // run this method every time the for loop is called, but it is not.
            reader.getRecords(index, regions[i][1], regions[i][2], checkForCallback);
        };

        // strip this method for readability
        checkForCallback = function(data) {
            d[i] = data;
            callbacksRemaining--;

            if (callbacksRemaining <= 0) {
                processData(d);
            }
        };
        // naming your functions like this makes it easy to know what's happening.
    });
Source Link
Randy
  • 312
  • 1
  • 9

I was posting about the same answer you did, but I inspired this review on your answer, not your question.

var regions = [
    [10, 10000, 100000],
    [12, 10000, 100000],
    [14, 10000, 100000],
    [18, 10000, 100000]
];


var d = [];
var readers = [];
var callbacksRemaining = regions.length;

regions.forEach(function(item, i) {
    // strip the function from your forEach loop for code control.
    // try not to nest loops if you don't need to.
    readers[i] = new readBinaryVCF(tabix, vcf, nameThisProperly);
});

function nameThisProperly(tabixReader) {

    // add var here
    // you're not in strict mode so this gets pushed to the global scope
    // in strict mode you get errors.
    var reader = readers[i];

    // you can use reversed psychology for this loop:
    var index = 0;
    for (var key in tabixReader.idxContent.namehash) {
        if (key == regions[i][0]) {
            index = tabixReader.idxContent.namehash[key];
        }
    }
    // example:
    var index = tabixReader.idxContent.namehash[regions[i][0]];
    // obviously untested, but something like this should work.
    

    // using index after the for loop seems weird. I would rename this variable, if it is only used to
    // work with the last element of the loop. Make it a good one ;) Now its like you would
    // run this method every time the for loop is called, but it is not.
    reader.getRecords(index, regions[i][1], regions[i][2], checkForCallback);
}

// strip this method for readability
function checkForCallback(data) {
    d[i] = data;
    callbacksRemaining--;

    if (callbacksRemaining <= 0) {
        processData(d);
    }
}
// naming your functions like this makes it easy to know what's happening.