From 1f7ed39463b5bdbd127364a6b035c778ca474cb0 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Wed, 5 Jul 2017 19:49:03 -0400 Subject: [PATCH 1/4] did Promise warm-up --- index.js | 9 +++++++++ package.json | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 index.js create mode 100644 package.json diff --git a/index.js b/index.js new file mode 100644 index 0000000..0fb6e6e --- /dev/null +++ b/index.js @@ -0,0 +1,9 @@ +var p = new Promise(function(resolve, reject) { + setTimeout(function() { + resolve("Hello world!"); + }, 1000); +}); + +p.then(function(message) { + console.log(message); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..9664fb3 --- /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/Avonyel/assignment_async_nodejs.git" + }, + "author": "Stephanie Barker", + "license": "ISC", + "bugs": { + "url": "https://github.com/Avonyel/assignment_async_nodejs/issues" + }, + "homepage": "https://github.com/Avonyel/assignment_async_nodejs#readme" +} From 33e4d82512517c448e8c6d54d48f2fdd88e6e1d5 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Thu, 6 Jul 2017 10:35:06 -0400 Subject: [PATCH 2/4] completed square function and Promise.all --- index.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 0fb6e6e..057d37b 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,57 @@ -var p = new Promise(function(resolve, reject) { - setTimeout(function() { - resolve("Hello world!"); - }, 1000); +delay = function(milliseconds) { + var p = new Promise(function(resolve, reject) { + setTimeout(function() { + resolve(milliseconds); + }, milliseconds) + }); + + return p; +}; + +countDown = function(milliseconds) { + if (milliseconds > 0) { + console.log(milliseconds); + milliseconds -=100; + } else { + console.log("Done!"); + }; + + return delay(milliseconds); +}; + +delay(1000) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown) + .then(countDown); + +var square = function(number) { + var p = new Promise(function (resolve, reject) { + if (typeof number === "number") { + var newNum = number * number; + resolve(newNum); + } else { + reject("Not a number"); + } + }); + + return p; +}; + +var numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9] + +numArray = numArray.map(function(num) { + return square(num); }); -p.then(function(message) { - console.log(message); -}); \ No newline at end of file +Promise.all(numArray) + .then(function(number) { + console.log(number); + }); \ No newline at end of file From 324af068e948f8e47ab92ca1092ac7c0c4a5bac1 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Thu, 6 Jul 2017 10:47:03 -0400 Subject: [PATCH 3/4] finished working with reject handlers and catch --- index.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/index.js b/index.js index 057d37b..dcf64da 100644 --- a/index.js +++ b/index.js @@ -54,4 +54,33 @@ numArray = numArray.map(function(num) { Promise.all(numArray) .then(function(number) { console.log(number); + }); + +doBadThing = function(forRealz) { + var p = new Promise(function(resolve, reject) { + if (!forRealz) { + resolve("Yay!"); + } else { + reject("Oh noooooo!") + }; + }); + + return p; +}; + +doBadThing(true) + .then(function(result) { + console.log(result); + }) + .catch(function(err) { + console.error(err) + }); + +doBadThing(false) + .then(function(result) { + throw "Whoops~!"; + console.log(result); + }) + .catch(function(err) { + console.error(err) }); \ No newline at end of file From 3166d89e955d37c08f92e1d7d039e632ff1343b1 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Thu, 6 Jul 2017 11:24:15 -0400 Subject: [PATCH 4/4] created promise-based file system functions --- data/test.txt | 1 + index.js | 35 ++++++++++++++++++++++++++++++++++- lib/fsp.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 data/test.txt create mode 100644 lib/fsp.js 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/index.js b/index.js index dcf64da..972ad25 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,7 @@ +const fsp = require("./lib/fsp") + +console.log(fsp); + delay = function(milliseconds) { var p = new Promise(function(resolve, reject) { setTimeout(function() { @@ -83,4 +87,33 @@ doBadThing(false) }) .catch(function(err) { console.error(err) - }); \ No newline at end of file + }); + +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); + }); \ No newline at end of file diff --git a/lib/fsp.js b/lib/fsp.js new file mode 100644 index 0000000..258761d --- /dev/null +++ b/lib/fsp.js @@ -0,0 +1,47 @@ +const fs = require("fs"); + +const fsp = { + readFile: function(file) { + var p = new Promise(function(resolve, reject) { + fs.readFile(file, "utf8", function(err, data) { + if (err) { + reject(err); + } else { + resolve(data); + }; + }); + }); + + return p; + }, + + writeFile: function(file, text) { + var p = new Promise(function(resolve, reject) { + fs.writeFile(file, text, "utf8", function(err) { + if (err) { + reject(err); + } + + resolve(fsp.readFile(file)); + }); + }); + + return p; + }, + + appendFile: function(file, text) { + var p = new Promise(function(resolve, reject) { + fs.appendFile(file, text, "utf8", function(err) { + if (err) { + reject(err); + } + + resolve(fsp.readFile(file)); + }); + }); + + return p; + } +}; + +module.exports = fsp; \ No newline at end of file