From 986c379fa74ae8a53a0b8b48b34d5d90ef05ac56 Mon Sep 17 00:00:00 2001 From: Abhishek Kapoor Date: Mon, 12 Oct 2020 00:01:15 -0400 Subject: [PATCH 1/2] Added solutions for promises challenges --- README.md | 1 + promises.js | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 promises.js diff --git a/README.md b/README.md index f4ff6fd..d600e39 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,4 @@ 3. oop.js (http://csbin.io/oop) 4. async.js (http://csbin.io/async) 5. functional.js (http://csbin.io/functional) +6. promises.js (http://csbin.io/promises) \ No newline at end of file diff --git a/promises.js b/promises.js new file mode 100644 index 0000000..4bdde1e --- /dev/null +++ b/promises.js @@ -0,0 +1,101 @@ +// Challenge 1 +function sayHello() { + setTimeout(() => console.log('Hello!'), 1000); +} + +// Uncomment the line below when ready +// sayHello(); // should log "Hello" after 1000ms + + +// Challenge 2 +var promise = new Promise(function (resolve, reject) { + setTimeout(() => resolve('Resolved!'), 1000); +}); + +// Should print out "Resolved!" +// ADD CODE HERE +// promise.then(val => console.log(val)); + + +// Challenge 3 +promise = new Promise(function(resolve, reject) { + // ADD CODE HERE + reject('Reject!') +}) + +// Should print out "Reject!" +// ADD CODE HERE +// promise.then(val => console.log(val)).catch(val => console.log(val)); + + +// Challenge 4 +promise = new Promise(function (resolve, reject) { + // ADD CODE HERE + resolve('Promise has been resolved!'); +}); + +// Uncomment the lines below when ready +// promise.then(val => console.log(val)); +// console.log("I'm not the promise!"); + + +// Challenge 5 +function delay(){ + const promise = new Promise(function(resolve, reject) { + setTimeout(() => resolve(), 1000) + }); + return promise; +} + +// Uncomment the code below to test +// This code should log "Hello" after 1000ms +// delay().then(sayHello); + + +// Challenge 6 +// +// ADD CODE BELOW +var thirdPromise = new Promise(function(resolve, reject) { + resolve('Third Promise Resolved!') +}); + +var secondPromise = new Promise(function(resolve, reject) { + resolve(thirdPromise); + // reject('second promise rejected!!'); +}); +var firstPromise = new Promise(function(resolve, reject) { + resolve(secondPromise) +}); + +// Uncomment the code below to test +// Should log the resolved message from thirdPromise +// firstPromise.then(val => console.log(val)).catch(val => console.log(val)); + + +// Challenge 7 +const fakePeople = [ + { name: 'Rudolph', hasPets: false, currentTemp: 98.6 }, + { name: 'Zebulon', hasPets: true, currentTemp: 22.6 }, + { name: 'Harold', hasPets: true, currentTemp: 98.3 }, +] + +const fakeAPICall = (i) => { + const returnTime = Math.floor(Math.random() * 1000); + return new Promise((resolve, reject) => { + if (i >= 0 && i < fakePeople.length) { + setTimeout(() => resolve(fakePeople[i]), returnTime); + } else { + reject({ message: "index out of range" }); + } + }); +}; + +function getAllData() { + // CODE GOES HERE + const promise = Promise.all([fakeAPICall(0), fakeAPICall(1), fakeAPICall(1)]); + promise.then(val => console.log(val)).catch(err => console.log(err)); +} + +// Uncomment the code below to test +// Should log the response from fakePeople array +// getAllData(); \ No newline at end of file From bdab16d6b2060adb18031c7664a28830fbc287b2 Mon Sep 17 00:00:00 2001 From: Abhishek Kapoor Date: Mon, 12 Oct 2020 00:05:51 -0400 Subject: [PATCH 2/2] Fixed indentation --- promises.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/promises.js b/promises.js index 4bdde1e..3940f55 100644 --- a/promises.js +++ b/promises.js @@ -1,6 +1,6 @@ // Challenge 1 function sayHello() { - setTimeout(() => console.log('Hello!'), 1000); + setTimeout(() => console.log('Hello!'), 1000); } // Uncomment the line below when ready @@ -41,9 +41,9 @@ promise = new Promise(function (resolve, reject) { // Challenge 5 function delay(){ - const promise = new Promise(function(resolve, reject) { - setTimeout(() => resolve(), 1000) - }); + const promise = new Promise(function(resolve, reject) { + setTimeout(() => resolve(), 1000) +}); return promise; }