From 2bb16fef0bcdad7883d9ff92a2666429ec2c2add Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Fri, 27 Oct 2017 14:59:35 -0500 Subject: [PATCH 1/3] warm up mostly complete, file ops finished --- .DS_Store | Bin 0 -> 6148 bytes data/lorem.txt | 1 + data/test.txt | 1 + fsp.js | 42 ++++++++++++ index.js | 182 +++++++++++++++++++++++++++++++++++++++++++++++++ test.js | 17 +++++ 6 files changed, 243 insertions(+) create mode 100644 .DS_Store create mode 100644 data/lorem.txt create mode 100644 data/test.txt create mode 100644 fsp.js create mode 100644 index.js create mode 100644 test.js diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9175c0f245f8de5d1d6595ea19fc141cde0f89d8 GIT binary patch literal 6148 zcmeHKJ8r^25S<||kZ35AZmaa%V1=9$aDgB!B?SZu=-qL{oF{KSK_JIPnn*!!q?xxn zpI!SE9*>CV^3*Lv<{~nM8p_$sP%LgbkV$$PpxPPRx5a9+-yH^-;|{BBq?5HAW&K0_ zCAQnQZFf3uL_VLMKm5R}ST`Cwy#M-{#-ElEGC&5%02v?y|DOT$Y?J9Mkd_RP0Wxr5 zz`hR!YFHEdK>u{W_y_>B-*^|SeU< { + err ? console.log(error) : resolve(data); + }); + }) +}, + writeFile : function (filename, message) { + return new Promise ( function (resolve, reject) { + fs.writeFile(filename, message, 'utf8', (err,data) => { + resolve(data); + fsp.readFile(filename) + .then( data => { + console.log(data); + }) + }) + }) +}, + appendFile : function (filename, data) { + return new Promise ( function (resolve, reject) { + fs.appendFile (filename, data, 'utf8', (err,data) => { + resolve(data); + fsp.readFile(filename) + .then( data => { + console.log(data); + }) + }) + }) + } + + +} + + +module.exports = fsp; diff --git a/index.js b/index.js new file mode 100644 index 0000000..4bbb0c5 --- /dev/null +++ b/index.js @@ -0,0 +1,182 @@ +// +// // WARM UPS +// // +// // +// // +// // +// +// +// +// // warm up #1 +// // Create a promise that resolves the message "Hello Promise!" after 1 second +// +// var p = Promise.resolve('Hello Promise!'); +// +// +// p.then(function(message){ +// setTimeout(function(){ +// console.log(message); +// }, 1); +// }); +// +// +// // warm up #2 +// // Your delay function should return a promise that resolves the value +// // milliseconds after delaying for the specified number of milliseconds +// +// +// var delay = function (ms) { +// return new Promise( (resolve,reject) => { +// setTimeout ( resolve(ms) , ms) +// }); +// } +// +// var countDown = (remaining) => { +// if (remaining > 0 ) { +// console.log (remaining) +// remaining -= 100; +// return remaining; +// } else { +// console.log("Done!") +// } +// }; +// +// +// delay(1000) +// .then(countDown) //=> 1000 +// .then(countDown) //=> 900 +// .then(countDown) //=> 800 +// .then(countDown) //=> 700 +// .then(countDown) //=> 600 +// .then(countDown) //=> 500 +// .then(countDown) //=> 400 +// .then(countDown) //=> 300 +// .then(countDown) //=> 200 +// .then(countDown) //=> 100 +// .then(countDown); //=> Done! +// +// +// +// // warm up #3 +// // Create a function that accepts a number and returns a promise that resolves that number squared +// // The promise should reject if it is not passed a number +// // Now map an array of integers 1 to 9 to an array of promises using the function above +// // Use Promise.all to get the result of all of the promises in the array +// +// +// var squares = [1,2,3,4,5,6,7,8,9]; +// +// var squared = function (n) { +// return new Promise (function (resolve,reject) { +// if (typeof n === 'number') { +// resolve(n*n); +// } else { +// reject(n + " is not a number") +// } +// }); +// }; +// +// squares = squares.map (function (i) { +// return squared(i); +// }); +// +// Promise.all(squares) +// .then(function(result){ +// console.log(result + " is the result") +// }); +// +// +// // warm up #4 +// // ✔️ Create a function with this signature doBadThing(forRealz) +// // ✔️ Return a promise that resolves to "Yay!" when forRealz is falsy +// // ✔️ The promise should reject when forRealz is truthy +// // Now call doBadThing with both a true and false value chaining on .then and .catch +// // Experiment with using .catch vs supplying a reject handler in your .then call +// // Experiment using now try throwing an error in the resolve handler of your .then call +// // What do you notice about when the .catch handler and the reject handler are invoked? +// +// var doBadThing = function (value) { +// return new Promise (function (resolve,reject) { +// value == false ? resolve("Yay!") : reject (value + " => evalutes to true, got to rejection"); +// }); +// }; +// +// doBadThing(false).then (function(result) { +// console.log(result); +// }) +// .catch(function(err) { +// console.log(err); +// }) +// +// +// doBadThing(true).then (function(result) { +// console.log(result); +// }) +// .catch(function(err) { +// console.log(err); +// }) +// +// +// +// // Now call doBadThing with both a true and false value chaining on .then and .catch +// // **** come back to this **** +// // how to call two values? used map with array of 4 values, but only showing one +// // to the console +// +// var bad_things = [false, false, true, true] +// +// bad_things = bad_things.map ( i => { +// return doBadThing(i); +// }); +// +// +// Promise.all(bad_things).then (function(result) { +// console.log(result); +// }) +// .catch(function(err) { +// console.log(err); +// }) + +// Experiment with using .catch vs supplying a reject handler in your .then call +// with a rejected hanlder, its functioning as like an if/else statement + + +// FILE OPERATIONS +// +// +// +// +//This part of the assignment takes the fs module in Node.js and has you wrap it +//with a promise based interface. + +var fsp = require('./fsp'); + + +fsp.readFile('./data/lorem.txt') + .then(function(data) { + // Outputs the file data + console.log(data); + }) + .catch(function(err) { + console.error(err); + }); + +fsp.writeFile('./data/test.txt', 'Hello!') + .then(function(res) { + // Outputs the file data + // after writing + console.log(res); + }) + .catch(function(err) { + console.error(err); + }); + +fsp.appendFile('./data/test.txt', 'Hello again!') + .then(function(res) { + // Outputs the file data + // after appending + console.log(res); + }) + .catch(function(err) { + console.error(err); + }); diff --git a/test.js b/test.js new file mode 100644 index 0000000..914dd13 --- /dev/null +++ b/test.js @@ -0,0 +1,17 @@ +var testEven = new Promise(function(resolve, reject) { + if (Date.now() % 2 === 0) { + resolve('Even'); + } else { + reject('Odd'); + } +}); + + + +testEven + .then(function(result) { + console.log(result); + }) + .catch(function(err) { + console.error(err +" = >I'm an error!"); + }); From c6fdc8519d5e7ee3e1290dded81fab041d436e70 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Sun, 29 Oct 2017 17:04:22 -0500 Subject: [PATCH 2/3] emitter complete --- emitter.js | 82 ++++++++++++++ index.js | 308 ++++++++++++++++++++++++++++++----------------------- test.js | 29 ++--- 3 files changed, 272 insertions(+), 147 deletions(-) create mode 100644 emitter.js diff --git a/emitter.js b/emitter.js new file mode 100644 index 0000000..6bddfd4 --- /dev/null +++ b/emitter.js @@ -0,0 +1,82 @@ +// var Emitter = require('events'); +// +// +// var emitter = new Emitter(); +// +// emitter.on('party', function () { +// console.log('unce unce unce'); +// }); +// +// emitter.on('time', () => { +// console.log('synthesizer') +// }) +// +// emitter.removeListener('time', () => { +// console.log('synthesizer'); +// console.log(this) +// }) +// +// +// emitter.emit('party'); + +//events +//keys are event names +//values are functions +// when there are multiple functions, the functions are contained within an array + + +class Emitter { + constructor () { + this.domain = null; + this._events = {}; + this._eventsCount = 0; + this._maxListeners = undefined; + } + + on (eventType, callback) { + if (this._events[eventType] !== undefined) { + this._events[eventType] = [this._events[eventType], callback]; + } else { + this._events[eventType] = callback; + } + this._eventsCount = Object.keys(this._events).length; + } + + emit (eventType) { + if (this._events[eventType] !== undefined && typeof this._events[eventType] === 'object') { + this._events[eventType].forEach (function (i) { + i(); + }) + } else { + this._events[eventType](); + } + } + + removeListener(eventType, callback) { + if (eventType in this._events) { + if (typeof this._events[eventType] === 'object') { + var index = this._events[eventType].indexOf(callback); + this._events[eventType].splice(index,1); + } else { + delete this._events[eventType]; + } + } + this._eventsCount = Object.keys(this._events).length; + } + + + removeAllListeners(eventType) { + if (eventType in this._events) { + delete this._events[eventType]; + this._eventsCount += 1; + } + this._eventsCount = Object.keys(this._events).length; + } + +} + + + + + +module.exports = Emitter; diff --git a/index.js b/index.js index 4bbb0c5..c09139e 100644 --- a/index.js +++ b/index.js @@ -1,141 +1,140 @@ +// WARM UPS // -// // WARM UPS -// // -// // -// // -// // // // // -// // warm up #1 -// // Create a promise that resolves the message "Hello Promise!" after 1 second -// -// var p = Promise.resolve('Hello Promise!'); -// -// -// p.then(function(message){ -// setTimeout(function(){ -// console.log(message); -// }, 1); -// }); -// -// -// // warm up #2 -// // Your delay function should return a promise that resolves the value -// // milliseconds after delaying for the specified number of milliseconds -// -// -// var delay = function (ms) { -// return new Promise( (resolve,reject) => { -// setTimeout ( resolve(ms) , ms) -// }); -// } -// -// var countDown = (remaining) => { -// if (remaining > 0 ) { -// console.log (remaining) -// remaining -= 100; -// return remaining; -// } else { -// console.log("Done!") -// } -// }; -// -// -// delay(1000) -// .then(countDown) //=> 1000 -// .then(countDown) //=> 900 -// .then(countDown) //=> 800 -// .then(countDown) //=> 700 -// .then(countDown) //=> 600 -// .then(countDown) //=> 500 -// .then(countDown) //=> 400 -// .then(countDown) //=> 300 -// .then(countDown) //=> 200 -// .then(countDown) //=> 100 -// .then(countDown); //=> Done! -// -// -// -// // warm up #3 -// // Create a function that accepts a number and returns a promise that resolves that number squared -// // The promise should reject if it is not passed a number -// // Now map an array of integers 1 to 9 to an array of promises using the function above -// // Use Promise.all to get the result of all of the promises in the array -// -// -// var squares = [1,2,3,4,5,6,7,8,9]; -// -// var squared = function (n) { -// return new Promise (function (resolve,reject) { -// if (typeof n === 'number') { -// resolve(n*n); -// } else { -// reject(n + " is not a number") -// } -// }); -// }; -// -// squares = squares.map (function (i) { -// return squared(i); -// }); -// -// Promise.all(squares) -// .then(function(result){ -// console.log(result + " is the result") -// }); -// -// -// // warm up #4 -// // ✔️ Create a function with this signature doBadThing(forRealz) -// // ✔️ Return a promise that resolves to "Yay!" when forRealz is falsy -// // ✔️ The promise should reject when forRealz is truthy -// // Now call doBadThing with both a true and false value chaining on .then and .catch -// // Experiment with using .catch vs supplying a reject handler in your .then call -// // Experiment using now try throwing an error in the resolve handler of your .then call -// // What do you notice about when the .catch handler and the reject handler are invoked? -// -// var doBadThing = function (value) { -// return new Promise (function (resolve,reject) { -// value == false ? resolve("Yay!") : reject (value + " => evalutes to true, got to rejection"); -// }); -// }; -// -// doBadThing(false).then (function(result) { -// console.log(result); -// }) -// .catch(function(err) { -// console.log(err); -// }) -// -// -// doBadThing(true).then (function(result) { -// console.log(result); -// }) -// .catch(function(err) { -// console.log(err); -// }) -// -// -// -// // Now call doBadThing with both a true and false value chaining on .then and .catch -// // **** come back to this **** -// // how to call two values? used map with array of 4 values, but only showing one -// // to the console -// -// var bad_things = [false, false, true, true] -// -// bad_things = bad_things.map ( i => { -// return doBadThing(i); -// }); -// -// -// Promise.all(bad_things).then (function(result) { -// console.log(result); -// }) -// .catch(function(err) { -// console.log(err); -// }) + + + +// warm up #1 +// Create a promise that resolves the message "Hello Promise!" after 1 second + +var p = Promise.resolve('Hello Promise!'); + + +p.then(function(message){ + setTimeout(function(){ + console.log(message); + }, 1); +}); + + +// warm up #2 +// Your delay function should return a promise that resolves the value +// milliseconds after delaying for the specified number of milliseconds + + +var delay = function (ms) { + return new Promise( (resolve,reject) => { + setTimeout ( resolve(ms) , ms) + }); +} + +var countDown = (remaining) => { + if (remaining > 0 ) { + console.log (remaining) + remaining -= 100; + return remaining; + } else { + console.log("Done!") + } +}; + + +delay(1000) + .then(countDown) //=> 1000 + .then(countDown) //=> 900 + .then(countDown) //=> 800 + .then(countDown) //=> 700 + .then(countDown) //=> 600 + .then(countDown) //=> 500 + .then(countDown) //=> 400 + .then(countDown) //=> 300 + .then(countDown) //=> 200 + .then(countDown) //=> 100 + .then(countDown); //=> Done! + + + +// warm up #3 +// Create a function that accepts a number and returns a promise that resolves that number squared + // The promise should reject if it is not passed a number + // Now map an array of integers 1 to 9 to an array of promises using the function above + // Use Promise.all to get the result of all of the promises in the array + + +var squares = [1,2,3,4,5,6,7,8,9]; + +var squared = function (n) { + return new Promise (function (resolve,reject) { + if (typeof n === 'number') { + resolve(n*n); + } else { + reject(n + " is not a number") + } + }); + }; + +squares = squares.map (function (i) { + return squared(i); +}); + +Promise.all(squares) + .then(function(result){ + console.log(result + " is the result") + }); + + +// warm up #4 +// ✔️ Create a function with this signature doBadThing(forRealz) + // ✔️ Return a promise that resolves to "Yay!" when forRealz is falsy + // ✔️ The promise should reject when forRealz is truthy + // Now call doBadThing with both a true and false value chaining on .then and .catch + // Experiment with using .catch vs supplying a reject handler in your .then call + // Experiment using now try throwing an error in the resolve handler of your .then call + // What do you notice about when the .catch handler and the reject handler are invoked? + +var doBadThing = function (value) { + return new Promise (function (resolve,reject) { + value == false ? resolve("Yay!") : reject (value + " => evalutes to true, got to rejection"); + }); +}; + +doBadThing(false).then (function(result) { + console.log(result); +}) + .catch(function(err) { + console.log(err); + }) + + +doBadThing(true).then (function(result) { + console.log(result); +}) + .catch(function(err) { + console.log(err); + }) + + + +// Now call doBadThing with both a true and false value chaining on .then and .catch +// **** come back to this **** +// how to call two values? used map with array of 4 values, but only showing one +// to the console + +var bad_things = [false, false, true, true] + +bad_things = bad_things.map ( i => { + return doBadThing(i); +}); + + +Promise.all(bad_things).then (function(result) { + console.log(result); +}) + .catch(function(err) { + console.log(err); + }) // Experiment with using .catch vs supplying a reject handler in your .then call // with a rejected hanlder, its functioning as like an if/else statement @@ -180,3 +179,44 @@ fsp.appendFile('./data/test.txt', 'Hello again!') .catch(function(err) { console.error(err); }); + + + + +// CREATE AN EMITTER FROM SCRATCH +// +// +// +// + +// var Emitter = require('./emitter.js'); +// var emitter = new Emitter(); + +// #1 create new instance +var events = require ('events'); +var Emitter = require ('./emitter.js') + + +var myEmitter = new Emitter(); + + +// callback functions +var unce = function() {console.log('unce unce unce')} +var bass = () => {console.log('bass line')} +var wubs = () => {console.log('wub wub')} + +// #2 - 4 adding listeners and emitting +myEmitter.on('party', unce); +myEmitter.on('party', bass); +myEmitter.on('bass_drop', wubs); + +myEmitter.emit('party'); + + +// # 5- 7 removing listeners + +myEmitter.removeListener('bass_drop', wubs); +console.log(myEmitter); + +myEmitter.removeAllListeners('party') +console.log(myEmitter) diff --git a/test.js b/test.js index 914dd13..37f79e1 100644 --- a/test.js +++ b/test.js @@ -1,17 +1,20 @@ -var testEven = new Promise(function(resolve, reject) { - if (Date.now() % 2 === 0) { - resolve('Even'); - } else { - reject('Odd'); +class test { + constructor () { + this.a = "test"; + this.b = "exam"; } -}); +} -testEven - .then(function(result) { - console.log(result); - }) - .catch(function(err) { - console.error(err +" = >I'm an error!"); - }); +var example = new test (); + +example.a = [example.a,'b'] + + +var array = [1,2,2, 5, 9]; +var index = array.indexOf(5); + +if (2 in array) { + console.log('hi there') +} From 380ae06643a7408f3fe5e9031263cc63d29b0a57 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Sun, 29 Oct 2017 20:16:45 -0500 Subject: [PATCH 3/3] assignment completed --- emitter.js | 26 -------------------------- index.js | 22 ++++++---------------- 2 files changed, 6 insertions(+), 42 deletions(-) diff --git a/emitter.js b/emitter.js index 6bddfd4..b667f5e 100644 --- a/emitter.js +++ b/emitter.js @@ -1,29 +1,3 @@ -// var Emitter = require('events'); -// -// -// var emitter = new Emitter(); -// -// emitter.on('party', function () { -// console.log('unce unce unce'); -// }); -// -// emitter.on('time', () => { -// console.log('synthesizer') -// }) -// -// emitter.removeListener('time', () => { -// console.log('synthesizer'); -// console.log(this) -// }) -// -// -// emitter.emit('party'); - -//events -//keys are event names -//values are functions -// when there are multiple functions, the functions are contained within an array - class Emitter { constructor () { diff --git a/index.js b/index.js index c09139e..235e36a 100644 --- a/index.js +++ b/index.js @@ -86,9 +86,9 @@ Promise.all(squares) // warm up #4 -// ✔️ Create a function with this signature doBadThing(forRealz) - // ✔️ Return a promise that resolves to "Yay!" when forRealz is falsy - // ✔️ The promise should reject when forRealz is truthy +// Create a function with this signature doBadThing(forRealz) + // Return a promise that resolves to "Yay!" when forRealz is falsy + // The promise should reject when forRealz is truthy // Now call doBadThing with both a true and false value chaining on .then and .catch // Experiment with using .catch vs supplying a reject handler in your .then call // Experiment using now try throwing an error in the resolve handler of your .then call @@ -117,27 +117,17 @@ doBadThing(true).then (function(result) { -// Now call doBadThing with both a true and false value chaining on .then and .catch -// **** come back to this **** -// how to call two values? used map with array of 4 values, but only showing one -// to the console -var bad_things = [false, false, true, true] -bad_things = bad_things.map ( i => { - return doBadThing(i); -}); - - -Promise.all(bad_things).then (function(result) { +doBadThing(true).then (function(result) { console.log(result); + throw "Hi there." }) .catch(function(err) { console.log(err); }) -// Experiment with using .catch vs supplying a reject handler in your .then call -// with a rejected hanlder, its functioning as like an if/else statement + // FILE OPERATIONS