Skip to content

Instantly share code, notes, and snippets.

@auchomage
auchomage / appLearningMA.js
Last active July 24, 2016 13:31
callbacksModelAnswerLearning1
// dealing with the Location
var location = require('./learningLocation.js');
// dealing with the Weather
var weather = require('./learningWeather.js');
// test the script.
// this works!!
location(function(userLocation){
console.log('Location is: ', userLocation);
});
@auchomage
auchomage / file1.js
Last active July 20, 2016 12:08
Referring to a callback
// This file generates output
module.exports = function(callback) {
callback('This is a model answer provided by Andrew Mead.');
};
@auchomage
auchomage / decon_t1_calledFile.js
Created July 12, 2016 13:49
Using module.exports in node.js
// make data available to decon_t1_callingFile.js
module.exports = function(){
console.log('Output from file decon_t1calledFile.js');
};
@auchomage
auchomage / decon_t2_calledFile.js
Created July 10, 2016 22:26
Node.JS callbacks - question 1: I have 2 files (1) decon_t2_calledFile.js and (2) decon_t2_callingFile.js. My aim is to take the string output of file 1 (decon_t2_calledFile.js) and access through file 2 (decon_t2_callingFile.js))
module.exports = function(callback){
function () {
// Contents of this anonymous function will serve as output
// from this file
console.log('** Output from decon_t2_calledFile.js ** ');
}
};
@auchomage
auchomage / app.js
Last active May 29, 2016 23:28
Struggling with the concepts of callbacks used together with apis (application programming interfaces)l
/*
Using accessing an external local file ie one on this computer
*/
var weather = require('./weather.js');
var location = require('./location2.js');
/*
weather(function(currentWeather) {
currentWeather;
});
@auchomage
auchomage / json.json
Last active May 12, 2016 17:18
Problem ajax script: I can get data from an external file (cross site request), but when I try to convert it into javascript I keep getting an error.
{
"firstName": "Henry",
"lastName": "Lawson",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
@auchomage
auchomage / ex2_accessingJSONdata.html
Created May 2, 2016 16:19
Following a tutorial and am unable to get the code to work
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Example JSON Discoveryvip</title>
<meta name="description" content"Discoveryvip.com>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
@auchomage
auchomage / try-catch-questions.js
Last active May 1, 2016 21:30
Questions regarding 'try-catch' blocks in node.js
/*
The example provided had a 'throw new Error' statement in the try section
see below
*/
try {
throw new Error('Unable to do the thing you wanted');
} catch(e){
console.log(e.message);
} finally {
@auchomage
auchomage / try-catch-example2.js
Created April 30, 2016 19:30
try-catch example
function doWork(){
// throw an error that says unable to work
console.log("Unable to work!");
}
try {
// call doWork()
throw new Error(doWork());
} catch (e) {
console.log(e.message);
@auchomage
auchomage / app_v2.js
Last active May 1, 2016 21:08
Model answer, not working.
console.log('Starting password manager.');
var crypto = require('crypto-js');
var storage = require('node-persist'); // integrates node-persist module into this application
storage.initSync(); // prepares the computer to write and save variables
/*
var retrievedAccount = storage.getItemSync('account');
console.log("Account is ", retrievedAccount);
*/