diff --git a/promise-shop/1.js b/promise-shop/1.js new file mode 100644 index 0000000..bbf0ae9 --- /dev/null +++ b/promise-shop/1.js @@ -0,0 +1,2 @@ + +setTimeout(()=>console.log("TIMED OUT!"), 300); diff --git a/promise-shop/10.js b/promise-shop/10.js new file mode 100644 index 0000000..c5e0f08 --- /dev/null +++ b/promise-shop/10.js @@ -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); diff --git a/promise-shop/2.js b/promise-shop/2.js new file mode 100644 index 0000000..97b1136 --- /dev/null +++ b/promise-shop/2.js @@ -0,0 +1,7 @@ +'use strict'; + +var promise = new Promise( (fulfill, reject)=> + setTimeout( ()=>fulfill("FULFILLED!"), 300) +); + +promise.then(console.log); diff --git a/promise-shop/3.js b/promise-shop/3.js new file mode 100644 index 0000000..cd8d9fc --- /dev/null +++ b/promise-shop/3.js @@ -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); diff --git a/promise-shop/4.js b/promise-shop/4.js new file mode 100644 index 0000000..5d71316 --- /dev/null +++ b/promise-shop/4.js @@ -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)); diff --git a/promise-shop/5.js b/promise-shop/5.js new file mode 100644 index 0000000..53f06a3 --- /dev/null +++ b/promise-shop/5.js @@ -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'); diff --git a/promise-shop/6.js b/promise-shop/6.js new file mode 100644 index 0000000..908c28b --- /dev/null +++ b/promise-shop/6.js @@ -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); +}); diff --git a/promise-shop/7.js b/promise-shop/7.js new file mode 100644 index 0000000..0e6127b --- /dev/null +++ b/promise-shop/7.js @@ -0,0 +1,4 @@ +'use strict' + +first().then(second) + .then(console.log); diff --git a/promise-shop/8.js b/promise-shop/8.js new file mode 100644 index 0000000..5f11a1b --- /dev/null +++ b/promise-shop/8.js @@ -0,0 +1,9 @@ +'use strict' + +function attachTitle(name){ + return 'DR. ' + name; +} + +let promise = Promise.resolve("MANHATTAN"); + +promise.then(attachTitle).then(console.log); diff --git a/promise-shop/9.js b/promise-shop/9.js new file mode 100644 index 0000000..ffc9760 --- /dev/null +++ b/promise-shop/9.js @@ -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); diff --git a/promise-shop/package.json b/promise-shop/package.json new file mode 100644 index 0000000..9b324cb --- /dev/null +++ b/promise-shop/package.json @@ -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" +}