Skip to content

Instantly share code, notes, and snippets.

Last active June 11, 2018 03:00
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Asynchronous Queue
license: gpl-3.0

This 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:

    .defer(d3_request.json, "1.json")
    .defer(d3_request.json, "2.json")
    .defer(d3_request.json, "3.json")

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.

<!DOCTYPE html>
<meta charset="utf-8">
h1 {
text-align: center;
font-family: "Helvetica Neue";
font-size: 96px;
line-height: 350px;
<h1>1 + 2 + 3 = <span id="result">?</span></h1>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
.defer(d3.json, "1.json")
.defer(d3.json, "2.json")
.defer(d3.json, "3.json")
function ready(error, results) {
if (error) throw error;
document.querySelector("#result").textContent = results.reduce(sum, 0);
function sum(p, v) {
return p + v;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment