Monday, November 2, 2015

Javascript execution weird

var mydata = []
d3.csv("https://raw.githubusercontent.com/kindlychung/cytob/master/data/hg19.csv", function (err, data) {
    var data1 = data.filter(function (d) {
        return d.chr == "chr22";
    })
    var innerdata = [];
    for(var i = 0; i < data1.length; i++) {
        mydata.push(data1[i].cyto);
        innerdata.push(data1[i].cyto);
    }
    console.log(innerdata);
    console.log("inside", mydata);
})
console.log("outside", mydata);

Result:

outside []
test.js:11 ["p13", "p12", "p11.2", "p11.1", "q11.1", "q11.21", "q11.22", "q11.23", "q12.1", "q12.2", "q12.3", "q13.1", "q13.2", "q13.31", "q13.32", "q13.33"]
test.js:12 inside ["p13", "p12", "p11.2", "p11.1", "q11.1", "q11.21", "q11.22", "q11.23", "q12.1", "q12.2", "q12.3", "q13.1", "q13.2", "q13.31", "q13.32", "q13.33"]

It’s strange that the outside log is actually executed before the inside log.

0 comments: