Skip to content
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: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# assignment_async_nodejs
Async Node.js sprint


Steven Zarrella
1 change: 1 addition & 0 deletions data/testy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The mouse is a big blue rabbit fox hound nugget!!!! MEOWWWWW!!!
1 change: 1 addition & 0 deletions data/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Organic twee quinoa slow-carb biodiesel. Ethical kinfolk disrupt tousled, direct trade slow-carb salvia. Banh mi intelligentsia thundercats sartorial VHS schlitz hoodie. Bitters chia ennui bushwick, tilde hexagon cornhole paleo chambray tumblr etsy. XOXO locavore gochujang, occupy meh literally air plant. Cardigan bicycle rights letterpress, trust fund plaid blog mustache vexillologist bushwick slow-carb normcore gastropub tacos hammock. Gastropub bicycle rights ramps direct trade VHS. Copper mug 90's iceland, biodiesel enamel pin hot chicken art party occupy bitters scenester. Vexillologist master cleanse umami green juice.
182 changes: 182 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
"use strict";

const fs = require('fs'),
//Emitter = require('events');
Emitter = require('./modules/emitter');

// WARMUP 1
// let promise1 = new Promise(function(resolve, reject) {
// if (1===1) {
// resolve("Hello Promise!");
// } else {
// reject("UNIVERSE BROKEN.");
// }
// })
// .then(function(message) {
// setTimeout(function() {
// return;
// }, 1000);
// console.log(message);
// });


// // WARMUP 2
// let delay = function(milliseconds) {
// return new Promise(function(resolve, reject) {
// setTimeout(function() {
// resolve(milliseconds);
// }, milliseconds);
// });
// }

// let countDown = function(value) {
// let newValue = value - 100;
// if (newValue < 100) {
// newValue = 0;
// }
// console.log(newValue);
// return new Promise(function(resolve, reject) {
// resolve(delay(newValue));
// });
// }

// delay(1000)
// .then(countDown)
// .then(countDown)
// .then(countDown)
// .then(countDown)
// .then(countDown)
// .then(countDown)
// .then(countDown)
// .then(countDown)
// .then(countDown)
// .then(countDown)
// .catch(function(error) {
// console.log(error);
// });


// // WARMUP 3
// let squaredPromise = function(number) {
// return new Promise(function(resolve, reject) {
// if (typeof number === 'number') {
// var numSquared = number * number;
// resolve(numSquared);
// } else {
// reject("NOT A NUMBER");
// }
// });
// }

// let numbers = [2, 3, 4, 5, 6];

// numbers = numbers.map(squaredPromise);

// Promise.all(numbers)
// .then(function(results) {
// console.log(results);
// })
// .catch(function(error) {
// console.log(error);
// });


// // WARMUP 4
// let doBadThing = function(forRealz) {
// return new Promise(function(resolve, reject) {
// if(forRealz == true) {
// resolve("Yay!");
// } else {
// reject("BOO.");
// }
// });
// }

// doBadThing(true)
// .then(function(result) {
// console.log(result);
// })
// .catch(function(error) {
// console.log(error);
// });


// // FILE OPERATIONS
// let fsp = {
// readFile: function(path) {
// return new Promise(function(resolve, reject) {
// var text = fs.readFile(path, 'utf8', (error, data) => {
// if (error) throw error;
// resolve(data);
// });
// });
// },

// writeFile: function(path, input) {
// return new Promise(function(resolve, reject) {
// var text = fs.writeFile(path, input, 'utf8', (error) => {
// if (error) reject(error);
// resolve(fsp.readFile(path));
// });
// });
// },

// appendFile: function(path, input) {
// return new Promise(function(resolve, reject) {
// var text = fs.appendFile(path, input, 'utf8', (error) => {
// if (error) reject(error);
// resolve(fsp.readFile(path));
// });
// });
// },
// }


// fsp.readFile('./data/text.txt')
// .then(function(data) {
// console.log(data);
// })
// .catch(function(error) {
// console.log(error);
// });

// fsp.writeFile('./data/testy.txt', "The mouse is a big blue rabbit fox hound nugget!!!!")
// .then(function(data) {
// console.log("File saved! CONTENTS: " + data);
// })
// .catch(function(error) {
// console.log(error);
// });

// fsp.appendFile('./data/testy.txt', " MEOWWWWW!!!")
// .then(function(data) {
// console.log("File saved! CONTENTS: " + data);
// })
// .catch(function(error) {
// console.log(error);
// });

// CREATE AN EVENT EMITTER FROM SCRATCH
let emitter = new Emitter();

emitter.on("meow", function(){console.log("foo meow");});
emitter.on("woof", function(){console.log("foo woof");});
emitter.on("click", function(){console.log("foo click");});
emitter.on("woof", function(){setTimeout(function() {console.log("WOOF woof")}
, 3000)});
emitter.on("woof", function(){console.log("KITTYCAT woof");});


emitter.emit("woof");

//emitter.removeListener("woof", woofWoof);

emitter.removeAllListeners("woof");








108 changes: 108 additions & 0 deletions modules/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"use strict";

//create Emitter constructor
function Emitter(eventType, callback) {

this.listeners = [];

this.executeCallback = function(data) {
if (data.length !== 0) {
data.forEach(function(item) {
return new Promise(function(resolve, reject) {
if (item) {
resolve(item.callback());
} else {
reject("No callback found!");
}
});
});
} else {
throw new Error("Event not found!");
}
};

this.on = function(eventType, callback) {
this.listeners.push({
"eventType": eventType,
"callback": callback
});
}; //this.on

this.emit = function(event) {

let listeners = this.listeners;
let matchArray = [];

//create the promise to execute each matching callback asynchronously
let executeCallback = function(data) {
if (data.length !== 0) {
return new Promise(function(resolve, reject) {
if (data) {
resolve(data.callback());
} else {
reject("No callback found!");
}
});
} else {
throw new Error("Event not found!");
}
};

//cycle through each listener to look for a matching event
listeners.forEach(function(item) {
if (item.eventType === event) {
matchArray.push(item);
};
});

//execute each matching event's callback function
if (matchArray.length === 0) {
throw new Error("Event not found.");
} else {
matchArray.forEach(function(item) {
executeCallback(item);
});
console.log("Success! Event(s) triggered.");
}

}; //this.emit

this.removeListener = function(event, callFunc) {

let eventString = event.toString() + callFunc.toString();

this.listeners = this.listeners.filter(function(item) {
let currentString = item.eventType.toString() + item.callback.toString();
console.log(currentString);
if (currentString !== eventString) {
return item;
}
});
console.log("Current listeners: " + this.listeners);
}; //this.removelistener

this.removeAllListeners = function(event) {
this.listeners = this.listeners.filter(function(item) {
if (item.eventType !== event) {
return item;
}
});

}; //this.removeAllListeners
}

module.exports = Emitter;














19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "assignment_async_nodejs",
"version": "1.0.0",
"description": "Async Node.js sprint",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/stevenvz/assignment_async_nodejs.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/stevenvz/assignment_async_nodejs/issues"
},
"homepage": "https://github.com/stevenvz/assignment_async_nodejs#readme"
}