Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Last active March 11, 2026 09:35
Show Gist options
  • Select an option

  • Save samarpanda/a7ac5d878cd5473b453ebb5b77345a8d to your computer and use it in GitHub Desktop.

Select an option

Save samarpanda/a7ac5d878cd5473b453ebb5b77345a8d to your computer and use it in GitHub Desktop.
const parallelFetch = async (urls) => {
var data = await Promise.all(
urls.map((fetchParams) => {
const { url, options = {} } = fetchParams;
return fetch(url, options).then((response) => response.json());
})
);
return data;
}
export default parallelFetch;
type FetchParams = {
url: string;
options?: RequestInit;
};
async function parallelFetch<T = any>(urls: FetchParams[]): Promise<T[]> {
const data = await Promise.all(
urls.map(async ({ url, options = {} }) => {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`Fetch failed: ${response.status} ${response.statusText}`);
}
return response.json() as Promise<T>;
})
);
return data;
}
export default parallelFetch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment