liveBook · Manning

Then method

As explained in D3.js in Action, Third Edition

then() is a Promise method that registers callbacks for handling an asynchronous operation's fulfillment or rejection.

The then() method waits for an asynchronous Promise to finish before running a callback. In a D3 workflow, chaining then() to d3.csv() makes the complete, transformed dataset available for exploration and visualization. section 3.2.3 section 3.6

Use the callback to prepare data before creating the visualization:

  • Wait until the external dataset has fully loaded. section 3.6
  • Access the entire dataset as an array of objects. section 3.2.3
  • Measure or reorganize the data with length, d3.max(), d3.min(), d3.extent(), and sort(). section 3.2.3
  • Pass the prepared data to the visualization-building function. section 3.2.3
d3.csv("../data/data.csv", d => {
  return {
    technology: d.technology,
    count: +d.count
  };
}).then(data => {
  console.log(data);
});

Chains then() to d3.csv(), converts each row, and logs the complete dataset after loading finishes. section 3.2.3

Measure data before building a bar chart

After loading and converting the CSV rows, use the then() callback to log data.length, find the minimum and maximum counts, sort technologies by count, and call createViz(data).

How D3.js in Action, Third Edition covers it

How deep it goes
Explains Promise timing and callback access through a D3 workflow, stopping short of broader Promise behavior.
Examples
Uses a 33-row technology dataset, numeric count conversion, summary statistics, descending sorting, and a bar-chart handoff.
What it assumes
Assumes basic JavaScript callbacks and D3 loading functions while demonstrating asynchronous data availability from first principles.
What you can do after
Apply it via JavaScript and D3 to manipulate a complete external dataset after loading finishes.
How it connects
Connects JavaScript Promises with d3.csv(), d3.json(), data transformation, data measurement, and SVG visualization.

Also explained in

Compare how 5 books explain then method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.