From 33152cd7a6c557783f711025746cf14e3cc96414 Mon Sep 17 00:00:00 2001 From: Visiona Date: Tue, 10 Oct 2017 19:43:15 +0100 Subject: [PATCH 1/6] Signup new project --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c32c34d..831b839 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # assignment_async_nodejs Async Node.js sprint + +Dariusz Biskupski From 464b2881fa22e673c6228bd31ee2a894d8680c46 Mon Sep 17 00:00:00 2001 From: Visiona Date: Tue, 10 Oct 2017 21:40:03 +0100 Subject: [PATCH 2/6] Created few promised, callback functions, playing with resolve, reject, then, catch, throw --- package.json | 19 ++++++++++++ promise.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 package.json create mode 100644 promise.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..f8b4249 --- /dev/null +++ b/package.json @@ -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/Visiona/assignment_async_nodejs.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/Visiona/assignment_async_nodejs/issues" + }, + "homepage": "https://github.com/Visiona/assignment_async_nodejs#readme" +} diff --git a/promise.js b/promise.js new file mode 100644 index 0000000..ee4aebb --- /dev/null +++ b/promise.js @@ -0,0 +1,85 @@ + + +var p = Promise.resolve("Hello Promise!"); + +p.then( function(message) { + console.log(message); +}); + + +var delay = function(milliseconds) { + return new Promise((resolve) => { + setTimeout(function() { + resolve(milliseconds); + }, milliseconds); + }); +} + +function countDown (message) { + console.log(message); + return message - 100; +} + +var square = function(number) { + return new Promise((resolve, reject) => { + resolve(number * number); + if (number == undefined) { + reject('No Number'); + } + }); +} + +var int = [1,2,3,4,5,6,7,8,9]; +var num = int.map( function(i) { + return square(i); +}) + +Promise.all(num).then(function(results) { + console.log(results); +}) + +var doBadThing = function(forRealz){ + return new Promise((resolve, reject) => { + if (forRealz == false) { + resolve('Yay'); + } else { + reject('rejected'); + } + }); +} + +doBadThing(4).then( function(result) { + console.log(result); + }) + .catch( function(err){ + console.error(err); + }); + +doBadThing(true).then(function() { + console.log('I am falsy'); +}, function() { + console.error('I am truthy'); +}) + +doBadThing(0).then( function(result) { + console.log(result); + throw "Boom Boom Boom! Finito!!"; + }) + .catch( function(err){ + console.error(err); + }); + + + +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); From f3290d70345c632e7109998274945da825d6a04d Mon Sep 17 00:00:00 2001 From: Visiona Date: Wed, 11 Oct 2017 19:58:25 +0100 Subject: [PATCH 3/6] Created asynchronous file opreation in node.js --- data/lorem.txt | 1 + data/test.txt | 1 + file_op.js | 31 +++++++++++++++++++++++++++++++ fsp.js | 38 ++++++++++++++++++++++++++++++++++++++ promise.js | 1 - 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 data/lorem.txt create mode 100644 data/test.txt create mode 100644 file_op.js create mode 100644 fsp.js diff --git a/data/lorem.txt b/data/lorem.txt new file mode 100644 index 0000000..cee7a92 --- /dev/null +++ b/data/lorem.txt @@ -0,0 +1 @@ +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/file_op.js b/file_op.js new file mode 100644 index 0000000..02bc495 --- /dev/null +++ b/file_op.js @@ -0,0 +1,31 @@ + +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/fsp.js b/fsp.js new file mode 100644 index 0000000..70c3ab4 --- /dev/null +++ b/fsp.js @@ -0,0 +1,38 @@ + +var fs = require('fs'); + +// +// fsp.writeFile('./data/test.txt', 'Hello!') +// .then(function(res) { +// // Outputs the file data +// // after writing +// console.log(res); +// }) +// .catch(function(err) { +// console.error(err); +// }); + +module.exports = { + readFile: function (path) { + return new Promise((resolve, reject) => { + fs.readFile(path, 'utf8', (err, data) => { + err ? reject(err) : resolve(data); + }) + }) + }, + writeFile: function (path, text) { + return new Promise((resolve, reject) => { + fs.writeFile(path, text,'utf8', (err, data) => { + err ? reject(err) : resolve(data); + }) + }) + }, + appendFile: function (path, text) { + return new Promise((resolve, reject) => { + fs.appendFile(path, text, 'utf8', (err, data) => { + err ? reject(err) : resolve(data); + }) + }) + } + +}; diff --git a/promise.js b/promise.js index ee4aebb..b9f5209 100644 --- a/promise.js +++ b/promise.js @@ -1,5 +1,4 @@ - var p = Promise.resolve("Hello Promise!"); p.then( function(message) { From f6b9b5a3132cdbe01f266b2654bcc9991d51e074 Mon Sep 17 00:00:00 2001 From: Visiona Date: Wed, 11 Oct 2017 21:50:00 +0100 Subject: [PATCH 4/6] Created simple node.js event emitter and tested it --- emitter.js | 21 +++++++++++++++++++++ event.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 emitter.js create mode 100644 event.js diff --git a/emitter.js b/emitter.js new file mode 100644 index 0000000..629e32a --- /dev/null +++ b/emitter.js @@ -0,0 +1,21 @@ +// var Emitter = require('./event'); + +var Emitter = require('events'); +var emitter = new Emitter(); + +var callback1 = function callback1() { + console.log('The first event occured!'); +} + +var callback2 = function callback2() { + console.log('A second Event!!'); +} +emitter.on('event', callback1 ) + .on('event', callback2 ); +emitter.emit('event'); + +emitter.removeListener('event', callback1 ); +console.log(emitter); + +emitter.removeAllListeners('event'); +console.log(emitter); diff --git a/event.js b/event.js new file mode 100644 index 0000000..4abf42c --- /dev/null +++ b/event.js @@ -0,0 +1,33 @@ + +var Emitter = function() { + this._events ={}; + this.on = function (eventType, callback) { + if (this._events[eventType]) { + this._events[eventType].push(callback); + } else { + this._events[eventType] = [callback]; + } + return this; + }; + this.emit = function(eventType) { + this._events[eventType].forEach( function(callback) { + return callback(); + }) + }; + this.removeListener = function(eventType, callback) { + var idx; + this._events[eventType].forEach( function(callback_iter, index) { + if (callback_iter === callback) { + idx = index + } + }) + this._events[eventType].splice(idx, 1); + }; + this.removeAllListeners = function(eventType) { + delete this._events[eventType]; + } +} + + + +module.exports = Emitter; From 05240ccea3af4cef64c2979d3208ab8da5406f8d Mon Sep 17 00:00:00 2001 From: Visiona Date: Mon, 23 Oct 2017 21:58:20 +0100 Subject: [PATCH 5/6] Updated README --- README.md | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 831b839..7eb29b1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,26 @@ -# assignment_async_nodejs -Async Node.js sprint +# Building with Async Node.js (promises, event emitter...) -Dariusz Biskupski +I this project I built promise-based interfaces. I used promises to write promise based versions of common Node.js fs module functions. Finally, `emitter.js` I created my own implementation of the Node.js EventEmitter and implemented it so it can be switched out with the core Node.js version! + + + +## Getting Started + +If you have [installed node](https://nodejs.org/en/download/) on your computer, type the following commands + +``` +$ node github_api.js +``` + +If you want to check statistics of other users run also the following lines after: + + + +## Authors + +* **Dariusz Biskupski** - *Initial work* - https://dariuszbiskupski.com + + +## Acknowledgments + +This assignment is created for [Viking Code School](https://www.vikingcodeschool.com/) From 073561b50af8f60291e08f1cb8ec04f31150503c Mon Sep 17 00:00:00 2001 From: Visiona Date: Mon, 23 Oct 2017 21:58:45 +0100 Subject: [PATCH 6/6] Updated README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7eb29b1..d406173 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ I this project I built promise-based interfaces. I used promises to write promis If you have [installed node](https://nodejs.org/en/download/) on your computer, type the following commands ``` -$ node github_api.js +$ node example.js ``` If you want to check statistics of other users run also the following lines after: