From 0a47ce9d0b05a6bc72750218ba13e9f09ddf7bd2 Mon Sep 17 00:00:00 2001 From: Uma Chandran Date: Fri, 23 Feb 2018 16:25:28 -0500 Subject: [PATCH] Adds solutions to promise exercises --- promises/exercise-1.js | 13 +++++++------ promises/exercise-2.js | 18 +++++++++++++++--- promises/exercise-3.js | 4 ++++ promises/exercise-4.js | 29 ++++++++++++++++++++++++----- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/promises/exercise-1.js b/promises/exercise-1.js index ca4dda8..25d40e4 100644 --- a/promises/exercise-1.js +++ b/promises/exercise-1.js @@ -4,13 +4,14 @@ require('es6-promise'); // Then, print the contents of the promise after it has been fulfilled by passing console.log to then. const promise = new Promise(function(resolve, reject) { - setTimeout(() => { - resolve('FULFILLED!'); - }, 300); + setTimeout(() => resolve('FULFILLED'), 300); }); -const onResolve = data => { - console.log(data); +const printWhenSuccessful = data => { + console.log('SUCCESS:', data); }; -promise.then(onResolve); +promise.then(printWhenSuccessful); + +// promise.then((value) => printWhenSuccessful(value)); + diff --git a/promises/exercise-2.js b/promises/exercise-2.js index e698a0c..82ac1e8 100644 --- a/promises/exercise-2.js +++ b/promises/exercise-2.js @@ -4,9 +4,21 @@ require("es6-promise"); // Create a function onReject to print error.message using console.log. Pass this function as a rejection handler to the then method of your promise. const promise = new Promise(function(resolve, reject) { - // Your solution here + const error = new Error('REJECTED!'); + setTimeout(() => reject(error), 300); }); -const onReject = value => { - // Your solution here +const printOnSuccess = value => { + console.log('SUCCESS:', value) }; + +const printOnFail = error => { + console.log('FAIL:', error.message); +}; + + +// promise.then(printOnSuccess, printOnFail); + +promise + .then(printOnSuccess) // printOnSuccess won't be called since our promise rejects + .catch(printOnFail); \ No newline at end of file diff --git a/promises/exercise-3.js b/promises/exercise-3.js index adba999..2631226 100644 --- a/promises/exercise-3.js +++ b/promises/exercise-3.js @@ -1,3 +1,7 @@ const fetch = require('node-fetch'); // Make a fetch request to https://next.json-generator.com/api/json/get/EJPkuFBIV + +fetch('https://next.json-generator.com/api/json/get/EJPkuFBIV') + .then(response => response.json()) + .then(data => console.log('data is', data)); \ No newline at end of file diff --git a/promises/exercise-4.js b/promises/exercise-4.js index 09d7d16..b2c97c8 100644 --- a/promises/exercise-4.js +++ b/promises/exercise-4.js @@ -18,22 +18,41 @@ const listOfWines = [ const myPromise = new Promise((resolve, reject) => { // Your solution for #1 here + setTimeout(() => resolve(listOfWines), 3000); }); const onSuccess = value => { - // Your solution here + console.log(value); + + + // extra challenge A solution: + // value + // .filter(wine => wine.type === 'Red') + // .forEach(wine => console.log(wine)); }; // Use myPromise here with onSuccess - + myPromise.then(onSuccess); const myFailedPromise = new Promise((resolve, reject) => { // Your solution for #3 here + setTimeout(() => reject('something is broken'), 3000); }); - const onReject = value => { - // Your solution here + const onReject = error => { + console.log(`Oh no, the request failed because ${error}`); }; - // use myFailedPromise here with onReject + myFailedPromise.catch(onReject); + + + // Extra challenge A & B combined together: + + myPromise.then( + wines => { + console.log('my filtered wines: '); + wines + .filter(wine => wine.type === 'Red') + .forEach(wine => console.log(wine)); + }); \ No newline at end of file