Skip to content

Commit

Permalink
Promises 1-10
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGorbanev committed Feb 4, 2017
1 parent e3df7f0 commit d874d8c
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions promise-shop/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

setTimeout(()=>console.log("TIMED OUT!"), 300);
23 changes: 23 additions & 0 deletions promise-shop/10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

function iterate (num) {
console.log(num);
return ++num;
};

function alwaysThrows () {
throw new Error("OH NOES");
};

Promise.resolve(iterate(1))
.then(iterate)
.then(iterate)
.then(iterate)
.then(iterate)
.then(alwaysThrows)
.then(iterate)
.then(iterate)
.then(iterate)
.then(iterate)
.then(iterate)
.then(null, console.log);
7 changes: 7 additions & 0 deletions promise-shop/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var promise = new Promise( (fulfill, reject)=>
setTimeout( ()=>fulfill("FULFILLED!"), 300)
);

promise.then(console.log);
11 changes: 11 additions & 0 deletions promise-shop/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

var promise = new Promise(function (fulfill, reject) {
setTimeout(()=>reject(new Error("REJECTED!")),300);
});

function onReject (error) {
console.log(error.message);
}

promise.then(null, onReject);
8 changes: 8 additions & 0 deletions promise-shop/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

let promise = new Promise(function(fulfill, reject){
fulfill("I FIRED");
reject(new Error("I DID NOT FIRE"));
})

promise.then(console.log, (error)=>console.log(error.message));
9 changes: 9 additions & 0 deletions promise-shop/5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

let promise = new Promise(function(fulfill, reject){
fulfill('PROMISE VALUE');
});

promise.then(console.log, null);

console.log('MAIN PROGRAM');
30 changes: 30 additions & 0 deletions promise-shop/6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var message;
var promise;

function randomBytes(n) {
return (Math.random() * Math.pow(256, n) | 0).toString(16);
}

message =
'A fatal exception ' + randomBytes(1) + ' has occurred at ' +
randomBytes(2) + ':' + randomBytes(4) + '. Your system\nwill be ' +
'terminated in 3 seconds.';

promise = Promise.reject(new Error(message));

promise.catch(function (err) {
var i = 3;

process.stderr.write(err.message);

setTimeout(function boom() {
process.stderr.write('\rwill be terminated in ' + (--i) + ' seconds.');
if (!i) {
process.stderr.write('\n..... . . . boom . . . .....\n');
} else {
setTimeout(boom, 1000);
}
}, 1000);
});
4 changes: 4 additions & 0 deletions promise-shop/7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict'

first().then(second)
.then(console.log);
9 changes: 9 additions & 0 deletions promise-shop/8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

function attachTitle(name){
return 'DR. ' + name;
}

let promise = Promise.resolve("MANHATTAN");

promise.then(attachTitle).then(console.log);
15 changes: 15 additions & 0 deletions promise-shop/9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

function parsePromised(someJson){
return new Promise( (fulfill, reject)=>{
try {
fulfill(JSON.parse(someJson));
}
catch(error){
console.log(error);
}
}
);
};

parsePromised(process.argv[2]).then(null, null);
11 changes: 11 additions & 0 deletions promise-shop/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "promise-shop",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

0 comments on commit d874d8c

Please sign in to comment.