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 new file mode 100644 index 0000000..972ad25 --- /dev/null +++ b/index.js @@ -0,0 +1,119 @@ +const fsp = require("./lib/fsp") + +console.log(fsp); + +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); +}); + +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) + }); + +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 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" +}