Complete the code to load data in parallel using Remix loaders.
export async function loader() {
const data1 = fetch('/api/data1');
const data2 = fetch('/api/data2');
const results = await Promise.[1]([data1, data2]);
return results;
}Promise.all waits for all promises to resolve and returns their results as an array.
Complete the code to extract JSON data from parallel fetch calls in Remix loader.
export async function loader() {
const [res1, res2] = await Promise.all([fetch('/api/one'), fetch('/api/two')]);
const data1 = await res1.[1]();
const data2 = await res2.json();
return { data1, data2 };
}The json() method parses the response body as JSON.
Fix the error in the Remix loader to correctly load data in parallel and parse JSON.
export async function loader() {
const data1 = fetch('/api/data1').[1](res => res.json());
const data2 = fetch('/api/data2').then(res => res.json());
const [json1, json2] = await Promise.all([data1, data2]);
return { json1, json2 };
}We must call json() on the resolved response, so use then(res => res.json()) to chain the promise.
Fill both blanks to create a Remix loader that fetches and parses JSON data in parallel.
export async function loader() {
const dataPromises = [fetch('/api/a').[1](), fetch('/api/b').[2]()];
const [a, b] = await Promise.all(dataPromises);
return { a, b };
}Both fetch calls need to parse their responses as JSON using json().
Fill all three blanks to create a Remix loader that fetches three APIs in parallel and returns their JSON data.
export async function loader() {
const promises = [
fetch('/api/x').[1](),
fetch('/api/y').[2](),
fetch('/api/z').[3]()
];
const [x, y, z] = await Promise.all(promises);
return { x, y, z };
}All three fetch calls must parse their responses as JSON using json().
