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/index.js b/index.js new file mode 100644 index 0000000..2aa1be6 --- /dev/null +++ b/index.js @@ -0,0 +1,10 @@ +var fsp = require('./lib/fsp'); + +fsp.readFile('./data/lorem.txt') + .then(function(data) { + // Outputs the file data + console.log(data); + }) + .catch(function(err) { + console.error(err); + }); diff --git a/lib/fsp.js b/lib/fsp.js new file mode 100644 index 0000000..b715e1e --- /dev/null +++ b/lib/fsp.js @@ -0,0 +1,24 @@ +var fs = require('fs'); + +var fsp = { + // readFile: function(path){ + // fs.readFile(path, 'utf8', function(err, data){ + // err ? console.log('err', err) : console.log(data); + // }) + // + // } + + readFile: function(path){ + fs.readFile(path, 'utf8', function(err, data){ + // console.log(data, 'data!') + // return Promise.resolve(data); + var x = Promise.resolve(data) + console.log(x, 'datayay') + return x; + }) + } + + +}; + +module.exports = fsp; diff --git a/warmups.js b/warmups.js new file mode 100644 index 0000000..7a9dd47 --- /dev/null +++ b/warmups.js @@ -0,0 +1,107 @@ +// warmups + +//warmup 1 +//version 1.0 +var promise1 = Promise.resolve("Hello Promise"); + +promise1.then(function(message){ + setTimeout(function(){ + console.log(message); + }, 1000); +}) + +//version 1.1 + +var promise2 = Promise.resolve("Hello Promise2"); + +promise2.then(function(message){ + setTimeout(function(){ + console.log(message); + }, 1000); +}); + +//warmup 2 + +var delay = function(miliseconds){ + setTimeout(function(){ + + }, miliseconds); + return Promise.resolve(miliseconds); +}; + + +var countDown = function(miliseconds){ + miliseconds > 0 ? console.log(miliseconds, "countDown fired") : console.log("Done!"); + return Promise.resolve(miliseconds - 100); +}; + +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! + +//warmup 3 +var oneToNine = [1, 2, 3, 4, 5, 6, 7, 8, 9]; + +var squared = function(n){ + if (!isNaN(n)) { + return Promise.resolve(n * n); + } +}; + +var newArray = oneToNine.map(function(i){ + return squared(i); +}); + +Promise.all(newArray) + .then(function(result){ + console.log(result); + }) + + +//warmup 4 + +var doBadThing = function(forReal){ + return forReal ? Promise.resolve("YAY!") : Promise.reject(forReal); +}; + +doBadThing(true) + .then(function(result){ + //the Promise.resolve() + console.log('result', result) + }, + function(err){ + //the Promise.reject() + console.log('err', err); + }); + + +//not using catch +doBadThing(false) + .then(function(result){ + console.log('result', result) + }, + function(err){ + console.log('err', err); + }); + + +//using catch +doBadThing(false) + .then(function(result){ + console.log('result', result) + + //using throw will fire .catch() + throw "gibberish" + + }).catch(function(err){ + console.log("catch", err); + })