Snippets Collections
// Function to fetch weather data from an API
const fetchWeather = () => {
  return fetch('https://api.example.com/weather')
    .then((response) => {
      if (!response.ok) {
        throw new Error('Failed to fetch weather data');
      }
      return response.json();
    })
    .then((data) => {
      return { temperature: data.temperature, condition: data.condition };
    });
};

// Function to fetch stock market data from an API
const fetchStocks = () => {
  return fetch('https://api.example.com/stocks')
    .then((response) => {
      if (!response.ok) {
        throw new Error('Failed to fetch stock data');
      }
      return response.json();
    })
    .then((data) => {
      return { symbol: data.symbol, price: data.price };
    });
};

// Function to fetch news headlines from an API
const fetchNews = () => {
  return fetch('https://api.example.com/news')
    .then((response) => {
      if (!response.ok) {
        throw new Error('Failed to fetch news data');
      }
      return response.json();
    })
    .then((data) => {
      return data.articles.map(article => ({ title: article.title }));
    });
};

// Fetch all data in parallel using Promise.all
Promise.all([fetchWeather(), fetchStocks(), fetchNews()])
  .then(([weatherData, stockData, newsData]) => {
    console.log('Weather:', weatherData);
    console.log('Stocks:', stockData);
    console.log('News:', newsData);

    // Now you can update the UI with all the data
    updateDashboard(weatherData, stockData, newsData);
  })
  .catch((error) => {
    console.error('Error fetching data:', error);
  });

// Example of how you could update the dashboard
function updateDashboard(weather, stocks, news) {
  console.log('Updating dashboard with all the data...');
  // Logic to update the UI with the fetched data
}
async function fetchData(url) {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`Error: ${response.status} ${response.statusText}`);
    }
    const data = await response.json(); // Parse and return JSON data
    return data;
  } catch (error) {
    console.error('Fetch failed:', error.message);
    throw error; // Re-throw the error if necessary for handling elsewhere
  }
}

async function useData() {
  try {
    const data1 = await fetchData('https://jsonplaceholder.typicode.com/posts/1');
    console.log('First Request Data:', data1);
  
    // If you need to fetch data from another endpoint:
    const data2 = await fetchData('https://jsonplaceholder.typicode.com/posts/2');
    console.log('Second Request Data:', data2);
  } catch (error) {
    console.error('Error in data retrieval:', error);
  }
}

useData();
version: "2"
authtoken: ....
tunnels:
  auth:
    addr: http://localhost:8001/
    host_header: localhost:8001
    proto: http
  conversation:
    addr: http://localhost:8002/
    host_header: localhost:8002
    proto: http

> ngrok start --all --config=ngrok.yml
star

Mon Sep 09 2024 05:40:46 GMT+0000 (Coordinated Universal Time)

#fetch #endpoint #data #promise.all
star

Mon Sep 09 2024 05:36:26 GMT+0000 (Coordinated Universal Time)

#fetch #endpoint #data
star

Mon Sep 02 2024 20:06:54 GMT+0000 (Coordinated Universal Time)

#ngrok #server #api #endpoint

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension