Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sub #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

sub #68

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/lorem.txt
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
});
24 changes: 24 additions & 0 deletions lib/fsp.js
Original file line number Diff line number Diff line change
@@ -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;
107 changes: 107 additions & 0 deletions warmups.js
Original file line number Diff line number Diff line change
@@ -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);
})