write a function to fetch data from given URLs in parallel

No Probleeem, i got you!


async function fetchDataInParallel(urls) {
    // Array to store promises for each fetch request
    const promises = [];

    // Start fetching data from each URL concurrently
    for (const url of urls) {
        // Fetch data from the current URL
        const promise = fetch(url)
            .then(response => {
                // If the response is not successful, throw an error
                if (!response.ok) {
                    throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
                // Parse and return the JSON data
                return response.json();
            .catch(error => {
                // Handle any errors during the fetch operation
                console.error(`Error fetching ${url}:`, error.message);
                // Return null to maintain the order of results
                return null;

        // Add the promise to the array

    try {
        // Wait for all promises to resolve concurrently
        const results = await Promise.all(promises);
        return results;
    } catch (error) {
        // Handle errors thrown during Promise.all
        console.error('Error fetching data in parallel:', error);
        return [];