From 57c3f230d32ff0cc6246a0382cf3b70e44b0fee8 Mon Sep 17 00:00:00 2001 From: "Brian P. Gallagher" Date: Tue, 3 Jul 2018 21:17:28 -0400 Subject: [PATCH 1/2] name added --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c32c34d..63c27df 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # assignment_async_nodejs Async Node.js sprint + +Brian P. Gallagher From a4d7b9e830bc19c73f8d935201e678842b5b2d36 Mon Sep 17 00:00:00 2001 From: "Brian P. Gallagher" Date: Fri, 6 Jul 2018 01:29:39 -0400 Subject: [PATCH 2/2] commit --- data/lorem.txt | 49 ++++++++++++++++ data/test.txt | 1 + events.js | 35 ++++++++++++ file_io.js | 49 ++++++++++++++++ lib/fsp.js | 82 +++++++++++++++++++++++++++ lib/my_event_emitter.js | 58 +++++++++++++++++++ promises.js | 123 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 397 insertions(+) create mode 100644 data/lorem.txt create mode 100644 data/test.txt create mode 100644 events.js create mode 100644 file_io.js create mode 100644 lib/fsp.js create mode 100644 lib/my_event_emitter.js create mode 100644 promises.js diff --git a/data/lorem.txt b/data/lorem.txt new file mode 100644 index 0000000..1abeac7 --- /dev/null +++ b/data/lorem.txt @@ -0,0 +1,49 @@ +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + + + + + + + + + + + diff --git a/data/test.txt b/data/test.txt new file mode 100644 index 0000000..b861aad --- /dev/null +++ b/data/test.txt @@ -0,0 +1 @@ +Hello!Hello again! \ No newline at end of file diff --git a/events.js b/events.js new file mode 100644 index 0000000..0929022 --- /dev/null +++ b/events.js @@ -0,0 +1,35 @@ +var Emitter = require('./lib/my_event_emitter'); +// var Emitter = require('events'); + + +var emitter = new Emitter(); +emitter.on('go', function() { + console.log(this); +}); + +console.log('1. ', emitter._events); + +var fn = function() { + console.log('Hi'); +}; + +emitter.on('go', fn); +// emitter.addListener('go', fn); + +console.log('2. ', emitter._events); + +emitter.removeListener('go', fn); + +console.log('3. ', emitter._events); + +emitter.on('go', fn); + +console.log('4. ', emitter._events); + +emitter.emit('go'); + +emitter.removeAllListeners('go'); + +console.log('5. ', emitter._events); + + diff --git a/file_io.js b/file_io.js new file mode 100644 index 0000000..e9790b3 --- /dev/null +++ b/file_io.js @@ -0,0 +1,49 @@ +var fs = require('fs'); +var fsp = require('./lib/fsp'); + + +const FILE_PATH = `${ __dirname }/data/lorem.txt`; +// console.log(FILE_PATH); + +var data = fs.readFileSync(FILE_PATH, 'utf8'); +// console.log(data); + + +// fs.readFile(FILE_PATH, 'utf8', function(err, data) { +// err ? console.error(err) : console.log(data); +// }); + + +fsp.readFile('./data/lorem.txt') + .then(function(data) { + console.log(data); + }) + .catch(function(err) { + console.error(err); + }); + + +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); + }); + + + + + + + + diff --git a/lib/fsp.js b/lib/fsp.js new file mode 100644 index 0000000..944479b --- /dev/null +++ b/lib/fsp.js @@ -0,0 +1,82 @@ +var fs = require('fs'); + + +var fsp = {}; + +// readFile(fd, options, cb) +fsp.readFile = function(path, options) { + if (!options) { + options = 'utf8'; + } + + return new Promise(function(resolve, reject) { + fs.readFile(path, options, function(err, data) { + err ? reject(err) : resolve(data); + }); + }); +}; + + +// writeFile(file, data, options, cb) +fsp.writeFile = function(path, options) { + if (!options) { + options = 'utf8'; + } + + return new Promise(function(resolve, reject) { + fs.writeFile(path, options, function(err) { + if (err) { + reject(err); + } else { + fsp.readFile(path) + .then(function(data) { + resolve(data); + }) + .catch(function(err) { + reject(err); + }); + } + }); + }); +}; + + +// appendFile(file, data, options, cb) +fsp.appendFile = function(path, data, options) { + if (!options) { + options = 'utf8'; + } + + return new Promise(function(resolve, reject) { + fs.appendFile(path, data, options, function(err) { + if (err) { + reject(err); + } else { + fsp.readFile(path) + .then(function(data) { + resolve(data); + }) + .catch(function(err) { + reject(err); + }); + } + }); + }); +}; + + +module.exports = fsp; + + + + + + + + + + + + + + diff --git a/lib/my_event_emitter.js b/lib/my_event_emitter.js new file mode 100644 index 0000000..935024e --- /dev/null +++ b/lib/my_event_emitter.js @@ -0,0 +1,58 @@ + + +function MyEventEmitter() { + this._events = {}; +} + + +MyEventEmitter.prototype.emit = function(eventType) { + if (!this._events[eventType]) { + return; + } + + for (var i = 0; i < this._events[eventType].length; i++) { + var callback = this._events[eventType][i]; + callback.call(this); + } +}; + + +MyEventEmitter.prototype.on = function(eventType, callback) { + this._events[eventType] = this._events[eventType] || []; + this._events[eventType].push(callback); +}; + + +MyEventEmitter.prototype.addListener = MyEventEmitter.prototype.on; + + +MyEventEmitter.prototype.removeListener = function(eventType, callback) { + if (this._events[eventType] && callback) { + for (var i = 0; i < this._events[eventType].length; i++) { + if (callback === this._events[eventType][i]) { + this._events[eventType].splice(i, 1); + } + } + } +}; + + +MyEventEmitter.prototype.removeAllListeners = function(eventType) { + this._events[eventType] = this._events[eventType] || []; + this._events[eventType].splice(0); + delete this._events[eventType]; +}; + + + + +module.exports = MyEventEmitter; + + + + + + + + + diff --git a/promises.js b/promises.js new file mode 100644 index 0000000..fe0f9a4 --- /dev/null +++ b/promises.js @@ -0,0 +1,123 @@ +// 1. Promise with setTimeout + +var p = new Promise(function(resolve) { + setTimeout(function() { + resolve('Hello Promise!'); + }, 1000); +}); + + +p.then(function(message) { + console.log(message); +}); + + + + +// 2. Promise driven delay + + +function delay(milliseconds) { + return new Promise(function(resolve) { + if (milliseconds > 0) { + setTimeout(function() { + resolve(milliseconds); + }, milliseconds); + } else { + resolve(0); + } + }); +} + +var countDown = function(t) { + var output = t > 0 ? t : 'Done!'; + console.log(output); + return delay(t - 100); +}; + +delay(1000) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown); + + + +// 3. Multiple promises with Promise.all + +var squares = [1, 2, 3, 4, 5, 6, 7, 8, 9]; +squares = squares.map(function(i) { + return new Promise(function(resolve, reject) { + if (Number.isNaN(i)) { + reject(`Cannot square: ${ i }`); + } else { + resolve(Math.pow(i, 2)); + } + }); +}); + + +Promise.all(squares) + .then(function(result) { + console.log(result); + }, function(error) { + console.log(error); + }); + + + +// 4. Catching errors with `catch` + +function doBadThing(forRealz) { + return new Promise(function(resolve, reject) { + if (forRealz) { + reject("Oh snap!"); + // throw new Error("Oh snap!"); + } else { + resolve("Yay!"); + } + }); +} + +doBadThing(true) + .then(function(message) { + console.log(message); + }) + .catch(function(err) { + console.error('Catch', err); + }); + +doBadThing(false) + .then(function(message) { + console.log(message); + throw "Fuh queue!"; + }, function(err) { + console.error('Reject', err); + }) + .catch(function(err) { + console.error('Catch', err); + }); + + + + + + + + + + + + + + + + +