JavaScript Project: Async Dashboard
An async dashboard gathers data from three sources and prints one summary. The three fetches run in parallel, and the dashboard prints when all three arrive.
Fetch one data address
fetch asks for the data at a data: URL. The response holds the JSON text, and response.json turns it into an object.
(async () => {
const response = await fetch("data:application/json,%7B%22users%22%3A42%7D");
const data = await response.json();
console.log(data.users);
})();Three fetches in parallel
Promise.all takes a list of promises and waits for all of them. The results arrive in the same order as the list.
(async () => {
const urls = [
"data:application/json,%7B%22users%22%3A42%7D",
"data:application/json,%7B%22orders%22%3A17%7D",
"data:application/json,%7B%22revenue%22%3A350%7D",
];
const results = await Promise.all(
urls.map(async function (url) {
const response = await fetch(url);
return response.json();
})
);
console.log(JSON.stringify(results));
})();Combine the results into one summary
The dashboard object gathers one number from each result. It prints as a single summary object.
(async () => {
const results = await Promise.all([
fetch("data:application/json,%7B%22users%22%3A42%7D").then(function (r) { return r.json(); }),
fetch("data:application/json,%7B%22orders%22%3A17%7D").then(function (r) { return r.json(); }),
]);
const dashboard = { users: results[0].users, orders: results[1].orders };
console.log(JSON.stringify(dashboard));
})();Example
(async () => {
const urls = [
"data:application/json,%7B%22users%22%3A42%7D",
"data:application/json,%7B%22orders%22%3A17%7D",
"data:application/json,%7B%22revenue%22%3A350%7D",
];
const results = await Promise.all(
urls.map(async function (url) {
const response = await fetch(url);
return response.json();
})
);
const dashboard = {
users: results[0].users,
orders: results[1].orders,
revenue: results[2].revenue,
};
console.log("Dashboard ready");
console.log("Users: " + dashboard.users);
console.log("Orders: " + dashboard.orders);
console.log("Revenue: " + dashboard.revenue);
})();Key points
- Promise.all: a method that waits until every promise in a list settles
- fetch: a function that asks for data and returns a promise of a response
- A data: URL holds its data inside the address, so no network is needed.
- Parallel fetches start together, so the dashboard waits only for the slowest one.
- Each response becomes usable data with response.json().
- An async function always returns a promise, so the dashboard code runs inside one.
In this example, the program fetches three data addresses in parallel with Promise.all. The dashboard object gathers the three numbers. The program prints the summary only after all three fetches arrive.
Note
Promise.all rejects as soon as any one of its promises rejects.
Try it Yourself
Edit the code, then press Run.
Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.
Exercise
Why does the dashboard use Promise.all?