liveBook · Manning

Promise

As explained in D3.js in Action, Third Edition

A JavaScript Promise is an object representing the eventual completion or failure of an asynchronous operation and its resulting value.

A Promise is an object representing the result of an asynchronous operation. In D3, d3.csv() returns a Promise while the browser loads an external dataset. section 3.2.3

Use then() to wait until loading finishes. Its callback receives the complete dataset, allowing you to inspect, measure, reorganize, and visualize the data. section 3.2.3 section 3.6

A Promise-based D3 data workflow can:

  • Transform each CSV row while loading, including converting count strings into numbers. section 3.2.3
  • Measure the complete array after loading with length, d3.max(), d3.min(), or d3.extent(). section 3.2.3
  • Sort the data and pass it to a visualization function only after it is available. section 3.2.3

Load and use a CSV dataset

d3.csv("../data/data.csv", d => ({ technology: d.technology, count: +d.count })).then(data => { console.log(data.length); data.sort((a, b) => b.count - a.count); createViz(data); });

d3.csv("../data/weekly_temperature.csv", d3.autoType).then(data => {
  console.log("temperature data", data);
});

Follow the complete Promise chain from loading and automatic type conversion to logging the finished dataset. section 4.1

How D3.js in Action, Third Edition covers it

How deep it goes
Defines the returned object and demonstrates then() callbacks, but does not develop rejection handling or Promise internals.
Examples
Uses d3.csv(), row conversion, d3.autoType, console logging, sorting, and createViz() with technology and temperature datasets.
What it assumes
Assumes basic JavaScript callbacks and D3 data loading while teaching how asynchronous availability controls later data operations.
What you can do after
Apply it via D3 to wait for loaded data before measuring, sorting, transforming, or drawing a visualization.
How it connects
Connects asynchronous operations with D3 data loading, callbacks, type conversion, dataset measurement, and visualization.

Also explained in

Compare how 7 books explain promise →

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.