Skip to content

Assignment Submission #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# assignment_async_nodejs
Async Node.js sprint

Jacob Farenci
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//Copy any of the lib files into here to run.
14 changes: 14 additions & 0 deletions lib/eventEmitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var Emitter = require('events');
var emitter = new Emitter();

function gotcha() {
console.log('Gotcha!');
}

emitter.on('emit', gotcha);

emitter.emit('emit');

emitter.removeListener('emit', gotcha);

emitter.emit('emit');
29 changes: 29 additions & 0 deletions lib/fileOperations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var fsp = require('./modules/fsp');

fsp.writeFile('./data/test.txt', 'Hello!')
.then(function(res) {
console.log(res);
})
.catch(function(err) {
console.error(err);
});

fsp.appendFile('./data/test.txt', ', Hello again!')
.then(function(res) {
console.log(res);
})
.catch(function(err) {
console.error(err);
});

setTimeout(function() {
fsp.readFile('./data/test.txt')
.then(function(data) {
console.log(data);
})
.catch(function(err) {
console.error(err);
});
}, 100);

console.log('Performing Operations...');
98 changes: 98 additions & 0 deletions lib/promises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
console.log('Starting up!');

//----------------------------------------------------------------------------->
//Do Bad Things
function doBadThing(forRealz) {
var doBadThing = new Promise(function(resolve, reject) {
if (forRealz) {
reject('error?');
} else {
resolve('Yay!');
}
});
return doBadThing;
}

//----------------------------------------------------------------------------->
//Squareing Map
function square(num) {
var square = new Promise(function(resolve, reject) {
if (Number.isInteger(num)) {
resolve(num * num);
} else {
reject('NaN error');
}
});
return square;
}

var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
nums = nums.map(function(i) {
return square(i);
});

//----------------------------------------------------------------------------->
//Countdown to Hello
var hello = Promise.resolve('Hello Promise!');

function delay(milliseconds, interval) {
var timer = new Promise(function(resolve, reject) {
resolve(milliseconds);
})
.then(function(result) {
countDown(result, interval);
});
}

function countDown(ms, int) {
console.log(ms);
var ticker = setInterval(function(){
if (ms <= (0 + int)) {
console.log('Done!')
clearInterval(ticker);
} else {
ms -= int;
console.log(ms);
}
}, int);
}

//----------------------------------------------------------------------------->
//Execution
hello.then(function(message){
setTimeout(function(){
console.log(message);
}, 1000);
})
.then(function(result) {
doBadThing(true)
.then(function(result) {
console.log(result);
})
.catch(function(err) {
console.log(err);
});
})
.then(function(result) {
doBadThing(NaN)
.then(function(result) {
console.log(result);
})
.catch(function(err) {
console.log(err);
});
})
.then(function(result) {
Promise.all(nums)
.then(function(result) {
console.log(result);
})
.catch(function(err) {
console.log(err);
});
})
.then(function(result){
delay(1000, 100);
});

console.log('Waiting...');
35 changes: 35 additions & 0 deletions modules/fsp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var fs = require('fs');

var fsp = {
readFile: function(path) {
var readF = new Promise(function(resolve, reject) {
fs.readFile(path, 'utf8', function(err, data) {
err ? reject('an error has occured')
: resolve(data+'\nread from '+path);
});
});
return readF;
},

writeFile: function(path, write) {
var writeF = new Promise(function(resolve, reject) {
fs.writeFile(path, write, function(err, data) {
err ? reject('an error has occured')
: resolve(write+' wrote to '+path);
});
});
return writeF;
},

appendFile: function(path, write) {
var appendF = new Promise(function(resolve, reject) {
fs.appendFile(path, write, function(err, data) {
err ? reject('an error has occured')
: resolve(write+' appended to '+path);
});
});
return appendF;
}
};

module.exports = fsp;
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "2_building_with_asynch_node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.5"
}
}