js fetch in then code example

Example 1: how to create a fetch function

const url = 'http://api.open-notify.org/astros.json'

const fetchurl = (url:string):void=>{

       fetch(url).then(res=>res.json()).then(jsonRes=>{
             
            console.log(jsonRes)
       })
}

fetchurl(url)

Example 2: fetch then then return value

function getvals(){
    return fetch('https://jsonplaceholder.typicode.com/posts',
    {
    	method: "GET",
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
    })
    .then((response) => response.json())
    .then((responseData) => {
      console.log(responseData);
      return responseData;
    })
    .catch(error => console.warn(error));
  }
  
  getvals().then(response => console.log(response));