Asynchronous Queue
This example demonstrates asynchronous loading and summation of three remote JSON files using the d3-queue library with a concurrency limit of two. A large headline displays the formula “1 + 2 + 3” and updates the result from “?” to the calculated sum once all files are fetched. The code uses `d3.queue(2)`, `d3.json` deferrals, and an `awaitAll` callback to render the computed total via `document.querySelector`. Data files `1.json`, `2.json`, and `3.json` each contain a single number, which are combined with a `reduce` function.
AI-generated descriptionThis is a demonstration of d3-queue, a tiny library for running asynchronous tasks with configurable concurrency. Here three external JSON files are loaded concurrently using d3-request, and then a ready function renders the sum of their contents:
d3_queue.queue(2)
.defer(d3_request.json, "1.json")
.defer(d3_request.json, "2.json")
.defer(d3_request.json, "3.json")
.await(ready);
The concurrency of the queue is set to two, such that the first two files (1.json and 2.json) are loaded in parallel. When either of those files has loaded, a third request is made for 3.json. See the API reference for details.
If you’d rather not load these modules separately, they are available as part of D3 4.0.