-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-promise.js
More file actions
38 lines (36 loc) · 1.22 KB
/
app-promise.js
File metadata and controls
38 lines (36 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const yargs = require('yargs');
const axios = require('axios');
const argv = yargs
.options({
a: {
demand: true,
alias: 'address',
describe: 'Address to fetch weather for',
string: true
}
})
.help()
.alias('help', 'h')
.argv;
var encodeAddress = encodeURIComponent(argv.address);
var geocodeUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeAddress}`;
axios.get(geocodeUrl).then(res => {
if (res.data.status === 'ZERO_RESULTS') {
throw new Error('Unable to find that address.');
}
var lat = res.data.results[0].geometry.location.lat;
var lng = res.data.results[0].geometry.location.lng;
var weatherUrl = `https://api.darksky.net/forecast/bd24b48f09651451b6de3f8c53396351/${lat},${lng}`;
return axios.get(weatherUrl);
}).then((response) => {
var temperature = response.data.currently.temperature;
var apparentTemperature = response.data.currently.apparentTemperature;
console.log(`It's currently ${temperature}. It feels like ${apparentTemperature}`);
}).catch(e => {
console.log(e);
if (e.code === 'ENOTFOUND') {
console.log('Unable to connect to API servers.');
} else {
console.log(e.message);
}
});