promises, and such
This commit is contained in:
parent
055eb9358a
commit
079d0e738e
47
app-promise.js
Normal file
47
app-promise.js
Normal file
@ -0,0 +1,47 @@
|
||||
const yargs = require('yargs');
|
||||
const axios = require('axios');
|
||||
|
||||
const credentials = require("./credentials")
|
||||
|
||||
const argv = yargs
|
||||
.options({
|
||||
a: {
|
||||
demand: true,
|
||||
alias: 'address',
|
||||
describe: "Address to fetch weather for.",
|
||||
string: true
|
||||
}
|
||||
})
|
||||
.help()
|
||||
.alias('help', 'h')
|
||||
.argv;
|
||||
|
||||
var encodedAddress = encodeURIComponent(argv.address);
|
||||
var geocodeUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}&key=${credentials.GOOGLE_API_KEY}`;
|
||||
|
||||
axios.get(geocodeUrl).then((response) => {
|
||||
if (response.data.status === "ZERO_RESULTS") {
|
||||
throw new Error('Unable to find that Address.')
|
||||
} else if (response.data.status === 'REQUEST_DENIED'){
|
||||
throw new Error(`API Request Denied: ${response.data.error_message}`);
|
||||
}
|
||||
var lat = response.data.results[0].geometry.location.lat;
|
||||
var lng = response.data.results[0].geometry.location.lng;
|
||||
console.log(response.data.results[0].formatted_address);
|
||||
var weatherUrl = `https://api.darksky.net/forecast/${API_KEY}/${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) => {
|
||||
if (e.code === 'ENOTFOUND') {
|
||||
console.log("Unable to connect to API servers");
|
||||
} else {
|
||||
console.log(e.message);
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
console.log("Powered By https://darksky.net/poweredby/");
|
1
app.js
1
app.js
@ -35,4 +35,3 @@ geocode.geocodeAddress(argv.address, (errorMessage, results) => {
|
||||
|
||||
|
||||
console.log("Powered By https://darksky.net/poweredby/");
|
||||
// ae8dd4c9c3a690289fd7cfe125f50e84
|
||||
|
7
credentials.js
Normal file
7
credentials.js
Normal file
@ -0,0 +1,7 @@
|
||||
var GOOGLE_API_KEY = process.env.GOOGLE_API_KEY;
|
||||
var DARKSKY_API_KEY = process.env.DARKSKY_API_KEY;
|
||||
|
||||
module.exports = {
|
||||
GOOGLE_API_KEY,
|
||||
DARKSKY_API_KEY
|
||||
}
|
35
package-lock.json
generated
35
package-lock.json
generated
@ -45,6 +45,14 @@
|
||||
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.6.0.tgz",
|
||||
"integrity": "sha1-g+9cqGCysy5KDe7e6MdxudtXRx4="
|
||||
},
|
||||
"axios": {
|
||||
"version": "0.13.1",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.13.1.tgz",
|
||||
"integrity": "sha1-Pmer/kMzvJ0tX+b70TtGlOr8jfg=",
|
||||
"requires": {
|
||||
"follow-redirects": "0.0.7"
|
||||
}
|
||||
},
|
||||
"bcrypt-pbkdf": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.1.tgz",
|
||||
@ -136,6 +144,14 @@
|
||||
"assert-plus": "1.0.0"
|
||||
}
|
||||
},
|
||||
"debug": {
|
||||
"version": "2.6.9",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
|
||||
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
}
|
||||
},
|
||||
"decamelize": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz",
|
||||
@ -192,6 +208,15 @@
|
||||
"pinkie-promise": "2.0.1"
|
||||
}
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "0.0.7",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-0.0.7.tgz",
|
||||
"integrity": "sha1-NLkLqyqRGqNHVx2pDyK9NuzYqRk=",
|
||||
"requires": {
|
||||
"debug": "2.6.9",
|
||||
"stream-consume": "0.1.0"
|
||||
}
|
||||
},
|
||||
"forever-agent": {
|
||||
"version": "0.6.1",
|
||||
"resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",
|
||||
@ -381,6 +406,11 @@
|
||||
"mime-db": "1.30.0"
|
||||
}
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
},
|
||||
"normalize-package-data": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz",
|
||||
@ -583,6 +613,11 @@
|
||||
"tweetnacl": "0.14.5"
|
||||
}
|
||||
},
|
||||
"stream-consume": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/stream-consume/-/stream-consume-0.1.0.tgz",
|
||||
"integrity": "sha1-pB6tGm1ggc63n2WwYZAbbY89HQ8="
|
||||
},
|
||||
"string-width": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz",
|
||||
|
@ -10,6 +10,7 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^0.13.1",
|
||||
"request": "^2.83.0",
|
||||
"yargs": "^4.8.1"
|
||||
}
|
||||
|
36
playground/promise.js
Normal file
36
playground/promise.js
Normal file
@ -0,0 +1,36 @@
|
||||
var asyncAdd = (a, b) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
if (typeof a === 'number' && typeof b === 'number'){
|
||||
resolve(a + b);
|
||||
} else {
|
||||
reject("Arguments must be numbers");
|
||||
}
|
||||
}, 500);
|
||||
|
||||
})
|
||||
}
|
||||
//
|
||||
//
|
||||
// var somePromise = new Promise((resolve, reject) => {
|
||||
// setTimeout(() => {
|
||||
// // resolve("Hey, It worked!");
|
||||
// reject("FAILED BRUH");
|
||||
// }, 500);
|
||||
// });
|
||||
//
|
||||
// somePromise.then((message) => {
|
||||
// console.log("Success:", message);
|
||||
// },
|
||||
// (errorMessage) => {
|
||||
// console.log("Error:", errorMessage);
|
||||
// })
|
||||
|
||||
|
||||
asyncAdd(1, 2).then((res) => {
|
||||
return asyncAdd(res, 33);
|
||||
}).then((res) => {
|
||||
console.log("Should be 36", res);
|
||||
}).catch((errorMessage) => {
|
||||
console.log(errorMessage);
|
||||
})
|
29
playground/promise2.js
Normal file
29
playground/promise2.js
Normal file
@ -0,0 +1,29 @@
|
||||
const request = require('request')
|
||||
|
||||
var geocodeAddress = (address) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
var encodedAddress = encodeURIComponent(address);
|
||||
request({
|
||||
url: `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}`,
|
||||
json: true
|
||||
}, (error, response, body) => {
|
||||
if (error) {
|
||||
reject("Unable to connect to Google Servers.");
|
||||
} else if (body.status === "ZERO_RESULTS"){
|
||||
reject("Unable to find that Address.");
|
||||
} else if (body.status === "OK"){
|
||||
resolve({
|
||||
address: body.results[0].formatted_address,
|
||||
latitude: body.results[0].geometry.location.lat,
|
||||
longitude: body.results[0].geometry.location.lng
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
geocodeAddress(19146).then((location) => {
|
||||
console.log(JSON.stringify(location, null, 2));
|
||||
}, (errorMessage) => {
|
||||
console.log(errorMessage);
|
||||
})
|
Loading…
Reference in New Issue
Block a user