Tip of the day - JavaScript

Un trucchetto al giorno per mantenerti allenato o per conoscere qualcosa di nuovo sul magico mondo di JavaScript.

108

Watch (and follow) the current position of the device with Geolocation API

2021-06-30 - JavaScript

1// JS Tip of the Day
2// Watch the current position of the device with Geolocation API
3
4let geoWatcher;
5const geoTarget = { lat: 37.869186, long: 41.275438 };
6const options = { enableHighAccuracy: true, timeout: 2000, maximumAge: 0 };
7
8function success({ coords }) {
9  if (geoTarget.lat === coords.latitude.toFixed(6) 
10      && geoTarget.long === coords.longitude.toFixed(6)) {
11   	
12  	console.log(`I'Batman! :D`);
13    navigator.geolocation.clearWatch(geoWatcher);
14  }
15}
16
17function error(err) {
18  console.warn(`Error: ${err.code} - ${err.message}`);
19}
20
21if (window.navigator && window.navigator.geolocation) {
22  geoWatcher = navigator.geolocation.watchPosition(success, error, options);
23}
#JStipoftheday
107

Create a range helper with Symbol.iterator

2021-06-29 - JavaScript

1// JS Tip of the Day
2// Create a range helper with Symbol.iterator
3
4let range = (from = 0, to = 5) => ({
5    from,
6    to,
7
8    [Symbol.iterator]() {
9        this.current = this.from;
10        return this;
11    },
12
13    next() {
14        if (this.current <= this.to) {
15            return { done: false, value: this.current++ };
16        } else {
17            return { done: true };
18        }
19    }
20});
21
22for (const item of range(10, 15)) {
23    console.log(item); // 10, 11, 12, 13, 14, 15
24}
#JStipoftheday
106

Get the current position of the device with Geolocation API

2021-06-28 - JavaScript

1// JS Tip of the Day
2// Get the current position of the device with Geolocation API
3
4const options = { enableHighAccuracy: true, timeout: 2000, maximumAge: 0 };
5
6function success({ coords }) {
7  console.log(`
8  	Lat: ${coords.latitude}
9    Long: ${coords.longitude}
10    Accuracy (meters): ${coords.accuracy}`
11  );
12}
13
14function error(err) {
15  console.warn(`Error: ${err.code} - ${err.message}`);
16}
17
18if (window.navigator && window.navigator.geolocation) {
19	navigator.geolocation.getCurrentPosition(success, error, options);
20}
#JStipoftheday
105

Promise.allSettled method

2021-06-25 - JavaScript

1// JS Tip of the Day
2// Promise.allSettled method
3
4const heroPromises = Promise.allSettled([
5  Promise.resolve('Batman'),
6  Promise.resolve('Superman'),
7  Promise.reject('Spiderman is not a DC Hero!')
8]);
9
10heroPromises.then(promises => {
11  promises.forEach(item => console.log(item));
12});
13
14// {status: "fulfilled", value: "Batman"}
15// {status: "fulfilled", value: "Superman"}
16// {status: "rejected", reason: "Spiderman is not a DC Hero!"}
#JStipoftheday
104

Promise.all method

2021-06-24 - JavaScript

1// JS Tip of the Day
2// Promise.all method
3
4const heroPromises = Promise.allSettled([
5  Promise.resolve('Batman'),
6  Promise.resolve('Superman')
7]);
8
9const anotherHeroPromises = Promise.allSettled([
10  Promise.resolve('Batman'),
11  Promise.reject('Spiderman is not a DC Hero!')
12])
13
14setTimeout(function() {
15  console.log(heroPromises);
16  // Promise { <state>: 'fullfilled', <value>: Array[2] }
17  
18  console.log(anotherHeroPromises);
19  // Promise { <state>: 'rejected', <reason>: 'Spiderman is not a DC Hero!' }
20});
#JStipoftheday

Preferisci ricevere i tips via mail? Iscriviti alla newsletter.

Iscriviti alla newsletter

Tip of the day - JavaScript

Hai in mente un progetto e vorresti realizzarlo?
Sei interessato a migliorare le tue competenze o quelle del tuo team in ambito di programmazione e sviluppo?

Anche se - semplicemente - vuoi prendere un caffè con noi o vedere la nostra collezione di Action Figures scrivici tramite questo form.

Questo sito è protetto da reCAPTCHA e si applicano le Norme sulla privacy e i Termini di servizio di Google.