Last active
March 11, 2026 09:35
-
-
Save samarpanda/a7ac5d878cd5473b453ebb5b77345a8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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