├── .gitignore ├── tips ├── test2.md ├── test1.md ├── test5.md ├── test6.md ├── test3.md └── test4.md ├── Pictures ├── Moses-Promise.png └── Promise-declare.jpeg ├── TIPS.md ├── package.json ├── index.js ├── test.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /tips/test2.md: -------------------------------------------------------------------------------- 1 | ## Can create a new Declare instance" 2 | * classes are used to create instances 3 | -------------------------------------------------------------------------------- /tips/test1.md: -------------------------------------------------------------------------------- 1 | ## Is not an instance of Promise 2 | * top tip - cheaters never win. this should right away. 3 | -------------------------------------------------------------------------------- /tips/test5.md: -------------------------------------------------------------------------------- 1 | ## Can chain thens 2 | * think about how one **then** can know what happened before, and use it. 3 | -------------------------------------------------------------------------------- /Pictures/Moses-Promise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/david35008/Promise-Challenge-Solution/HEAD/Pictures/Moses-Promise.png -------------------------------------------------------------------------------- /Pictures/Promise-declare.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/david35008/Promise-Challenge-Solution/HEAD/Pictures/Promise-declare.jpeg -------------------------------------------------------------------------------- /tips/test6.md: -------------------------------------------------------------------------------- 1 | ## Is asynchronous 2 | * consider what happens when in the code, and try to see what would make one thing happen after the other. 3 | -------------------------------------------------------------------------------- /tips/test3.md: -------------------------------------------------------------------------------- 1 | ## Should have "then" method which returns a Declare' 2 | * recursion is not only for functions... 3 | * it doesn't have to return a promise for everything to work, but it can help. 4 | -------------------------------------------------------------------------------- /tips/test4.md: -------------------------------------------------------------------------------- 1 | ## Can use the result of the declare using the "then" method' 2 | * every class has default values created by the constructor 3 | * try and understand what type of input the constructor and the **then** method take and why. 4 | 5 | -------------------------------------------------------------------------------- /TIPS.md: -------------------------------------------------------------------------------- 1 | ## tips for tests 2 | - [Is not an instance of Promise](tips/test1.md) 3 | - [Can create a new Declare instance"](tips/test2.md) 4 | - [Should have "then" method which returns a Declare'](tips/test3.md) 5 | - [Can use the result of the declare using the "then" method'](tips/test.md4) 6 | - [Can chain thens](tips/test5md) 7 | - [Is asynchronous](tips/test6.md) 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "first-ch", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "main.js", 6 | "scripts": { 7 | "test": "node_modules/.bin/jest" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/Cyber4sPopo/challengeme" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "devDependencies": { 16 | "jest": "^26.0.1" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | class Declare { 2 | constructor(CB = () => { }) { 3 | this.state = 'pending' 4 | this.value = null 5 | this.resolver = () => { } 6 | CB(this.resolve.bind(this)) 7 | } 8 | 9 | resolve(value) { 10 | if (this.state === 'pending') { 11 | this.state = 'resolved'; 12 | this.value = value 13 | this.resolver(value) 14 | } 15 | } 16 | 17 | then(CB = () => { }) { 18 | this.resolver = CB 19 | if (this.state === 'resolved') { 20 | return new Declare((resolveCB) => { 21 | resolveCB(CB(this.value)) 22 | }) 23 | } else { 24 | return this 25 | } 26 | } 27 | } 28 | module.exports = Declare 29 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const Declare = require("./"); 2 | const testValue = "Hanodrim-Rules"; 3 | 4 | jest.setTimeout(5000); 5 | 6 | const projectName = "Declare "; 7 | describe(projectName, () => { 8 | 9 | test("Is not an instance of Promise", () => { 10 | const declare = new Declare(()=>{}); 11 | return expect(declare instanceof Promise).toBe(false); 12 | }); 13 | 14 | test("Can create a new Declare instance", () => { 15 | const declare = new Declare(()=>{}); 16 | expect(declare).toBeDefined(); 17 | }); 18 | 19 | test('Should have "then" method which Resolves with a Declare', () => { 20 | const declare = new Declare(()=>{}); 21 | expect(typeof declare.then).toBe("function"); 22 | return expect(declare.then() instanceof Declare).toBe(true); 23 | }); 24 | 25 | test('Can use the result of the declare using the "then" method', () => { 26 | const declare = new Declare((resolve) => { 27 | resolve(testValue); 28 | }); 29 | return declare.then((result) => { 30 | expect(result).toBe(testValue); 31 | }); 32 | }); 33 | 34 | test("Can chain thens", () => { 35 | const declare = new Declare((resolve) => { 36 | resolve(5); 37 | }); 38 | return declare 39 | .then((res) => res + 5) 40 | .then((val) => { 41 | expect(val).toBe(10); 42 | }); 43 | }); 44 | 45 | test("Is asynchronous", (done) => { 46 | let changer = null; 47 | const declare = new Declare((resolve) => { //this declare should resolve with "5" after .5 seconds 48 | setTimeout(() => { 49 | resolve(testValue); 50 | }, 500); 51 | }); 52 | setTimeout(() => {// this checks after 1 second that the value of "changer" has changed 53 | expect(changer).toBe(testValue); 54 | done(); 55 | }, 1000); 56 | declare.then((value) => {// the promise will ( once it resolves) change "changer" into "5" 57 | changer = value; 58 | }); 59 | expect(changer).toBeNull();// this checks that the value of "changer" is stil null after the promise is created. 60 | }); 61 | 62 | }); 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Promise Challenge 2 | 3 | ![Promise=>declare](./Pictures/Promise-declare.jpeg) 4 | 5 | ## Story 6 | - The client comes from a religious background and isn't comfortable with the word "promise" being part of their software 7 | - The client wants a new class to replace the Promise, called... Declare 8 | - clone this Repo. 9 | - run `npm i` to install jest test library in order for the tests to run. 10 | - Edit [index.js](index.js) to implement the Declare class. 11 | - run `npm run test` in the terminal to run the tests. 12 | 13 | 14 | ## Requirements from the Declare class 15 | 1. is not implemented using the built-in Promise Class. 16 | 1. can create an instance with an **executor** function using the **new** keyword 17 | 1. has a **then** method, which returns a **Declare** instance 18 | 1. can use the return value of the **executor** with __then__ method 19 | 1. can use multiple **then**s chained one after the other 20 | 1. can use an asynchronous **executor** 21 | 22 | * **NOTE** - there's no need to handle errors in this implementation. 23 | #### HOT TIP 24 | add `.skip` to the end of a test in [test.js](test.js) to skip it in a run, and only test what you want to. 25 | ```javascript 26 | test.skip("Is not an instance of Promise", () => { 27 | const declare = new Declare(()=>{}); 28 | return expect(declare instanceof Promise).toBe(false); 29 | }); 30 | ``` 31 | 32 | ## Recommended prior Knowledge 33 | - How Promise works? 34 | [Promise MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) 35 | [Promises - why!? Callback-Hell VS Promises](https://medium.com/better-programming/callbacks-vs-promises-in-javascript-1f074e93a3b5) 36 | 37 | - JS classes + constructor: 38 | [class guide](https://javascript.info/class) 39 | [class MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor) 40 | 41 | - JS, how does it reads your code? 42 | [Asynchrony: Under the Hood](https://www.youtube.com/watch?v=SrNQS8J67zc&feature=youtu.be) 43 | 44 | ## HINTS 45 | [your stuck? click here!](TIPS.md) 46 | 47 | ![Moses-Promise](./Pictures/Moses-Promise.png) 48 | --------------------------------------------------------------------------------