Skip to content

Adds solutions to promise exercises #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions promises/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

18 changes: 15 additions & 3 deletions promises/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 4 additions & 0 deletions promises/exercise-3.js
Original file line number Diff line number Diff line change
@@ -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));
29 changes: 24 additions & 5 deletions promises/exercise-4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});