I'm following along in Adam Janes's choropleth tutorial.
The data is loaded with this code block
var promises = [
d3.json("https://d3js.org/us-10m.v1.json"),
d3.tsv("unemployment.tsv", function(d) { unemployment.set(d.id, +d.rate); })
]
Promise.all(promises).then(ready)
And ready is defined as
function ready([us]) {
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("fill", function(d) { return color(d.rate = unemployment.get(d.id)); })
.attr("d", path)
.append("title")
.text(function(d) { return d.rate + "%"; });
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr("d", path);
}
I understand this sequence as
first - make an array called promises where the first item is the parsed json from this link and the second item is a map with id/value pairs from the unemployment file
second - grab all promises in the promise variable and, if it succeeds, then trigger the function ready and if it fails, do nothing
If that's right, then what's the advantage over something like this? I'm writing this in pseudo code because I'm new to this
var promises = [
d3.json("https://d3js.org/us-10m.v1.json"),
d3.tsv("unemployment.tsv", function(d) { unemployment.set(d.id, +d.rate); })
]
if(promises == 'SUCCESS'){ function(ready) };
Just a note, I read about promises and their advantage since javascript is single-threaded. I ask this question since it doesn't strike me that anything asynchronous is happening, the code has to load the promises array in both cases.