Snippets Collections
async function getCurrentPosition() {
    return new Promise((resolve, reject) => {
      if (!navigator.geolocation) {
        reject("Geolocation is not supported");
        return;
      }

      navigator.geolocation.getCurrentPosition(
        (position) => {
          const { latitude, longitude } = position.coords;
          userLocation = { latitude, longitude }; // Store globally
          resolve(userLocation); // Resolve with user location
        },
        (error) => reject(error.message) // Reject on error (e.g., permission denied)
      );
    });
  }

  async function init() {
    // prettier-ignore
    try { // get user location  on page load 
      const location = await getCurrentPosition();
      console.log({userLocation});
    } catch (error) {
      console.error(error); 
    }
  }

  window.addEventListener("DOMContentLoaded", init);
const url ='http://sample.example.file.doc'
const authHeader ="Bearer 6Q************" 

const options = {
  headers: {
    Authorization: authHeader
  }
};
 fetch(url, options)
  .then( res => res.blob() )
  .then( blob => {
    var file = window.URL.createObjectURL(blob);
    window.location.assign(file);
  });
fetch("https://free-nba.p.rapidapi.com/teams?page=0", {
	"method": "GET",
	"headers": {
		"x-rapidapi-host": "free-nba.p.rapidapi.com",
		"x-rapidapi-key": "d1e022a733msh4e2c4a1ea244bf9p1a3971jsnea127bfc9297"
	}
})
.then((response) => {
  return response.json() // << This is the problem
})
.then((responseData) => { // responseData = undefined
  console.log(responseData.data);
 
})
.catch(err => {
	console.error(err);
});
star

Sat Mar 29 2025 22:29:45 GMT+0000 (Coordinated Universal Time)

#promise #pageload #init
star

Fri May 03 2024 18:15:39 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/32545632/how-can-i-download-a-file-using-window-fetch

#javascript #promise
star

Mon Aug 23 2021 18:14:18 GMT+0000 (Coordinated Universal Time)

#fetch #promise

Save snippets that work with our extensions

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