From dd9d69608b957b437b1338a7a522293d9ff16484 Mon Sep 17 00:00:00 2001 From: Kenny Kottenstette Date: Mon, 3 Sep 2018 22:30:07 -0700 Subject: [PATCH 1/3] begin warmup 3 --- index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..2e74ebe --- /dev/null +++ b/index.js @@ -0,0 +1,51 @@ +// 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 From 039cefd1d9374670ba2fa82ab54153fed830d1aa Mon Sep 17 00:00:00 2001 From: Kenny Kottenstette Date: Tue, 4 Sep 2018 00:00:33 -0700 Subject: [PATCH 2/3] begin file operations --- index.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 2e74ebe..7a9dd47 100644 --- a/index.js +++ b/index.js @@ -48,4 +48,60 @@ delay(1000) .then(countDown) //=> 100 .then(countDown); //=> Done! - //warmup 3 +//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); + }) From 8ee97fb9471cd25fcaa57912cacf4dbecdba6d96 Mon Sep 17 00:00:00 2001 From: Kenny Kottenstette Date: Tue, 4 Sep 2018 01:18:40 -0700 Subject: [PATCH 3/3] fsp trouble --- data/lorem.txt | 1 + index.js | 111 ++++--------------------------------------------- lib/fsp.js | 24 +++++++++++ warmups.js | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 104 deletions(-) create mode 100644 data/lorem.txt create mode 100644 lib/fsp.js create mode 100644 warmups.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/index.js b/index.js index 7a9dd47..2aa1be6 100644 --- a/index.js +++ b/index.js @@ -1,107 +1,10 @@ -// warmups +var fsp = require('./lib/fsp'); -//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); +fsp.readFile('./data/lorem.txt') + .then(function(data) { + // Outputs the file data + console.log(data); }) - - -//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); + .catch(function(err) { + console.error(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); - }) 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); + })