From 6a883eeaad53259e53bfbd79c9e914166a12fefe Mon Sep 17 00:00:00 2001 From: waynebanks Date: Wed, 19 Apr 2017 11:23:44 -0700 Subject: [PATCH 01/17] completed exexercises 1-7 --- pg-promise-exercises/exercises.js | 287 ++++++++++++++++++------------ 1 file changed, 171 insertions(+), 116 deletions(-) diff --git a/pg-promise-exercises/exercises.js b/pg-promise-exercises/exercises.js index c154c5b..af0e300 100644 --- a/pg-promise-exercises/exercises.js +++ b/pg-promise-exercises/exercises.js @@ -5,25 +5,25 @@ const postgresConfig = { host: 'localhost', port: 5432, database: 'pg-promise-exercises', - user: '', // replace this with your username + user: 'waynebanks', // replace this with your username password: '' // replace this if you have set a password for your username (this is unlikely) }; const db = pg(postgresConfig); -/* ----------------------------------------- - Exercise 1 - ----------------------------------------- + // ----------------------------------------- + // Exercise 1 + // ----------------------------------------- - This is an example function that finds all the books from the `books` table - @function: `allBooks` - @input params: None - @output: [{id, title, author_id, subject_id}] + // This is an example function that finds all the books from the `books` table + // @function: `allBooks` + // @input params: None + // @output: [{id, title, author_id, subject_id}] + + // The assertion fails, and you have to make it pass. - The assertion fails, and you have to make it pass. -*/ @@ -31,7 +31,7 @@ const allBooks = db.any('select * from books') /* This is calling the `then` function on the `allBooks` promise, and checks if we get back 15 rows. This assertion will fail. Make it PASS!*/ allBooks.then(books => { - assert.deepEqual(books.length, 20) + assert.deepEqual(books.length, 15) }).catch(error => { console.log('Dang, my assertion failed.', error); }); @@ -42,20 +42,20 @@ allBooks.then(books => { -/* ----------------------------------------- - Exercise 2 - ----------------------------------------- + // ----------------------------------------- + // Exercise 2 + // ----------------------------------------- + + // Implement the function `firstTenBooks` which returns just the names of the + // books, and make the assertion pass. + // @function: `firstTenBooks` + // @input params: None + // @output: [{id, title, author_id, subject_id}] - Implement the function `firstTenBooks` which returns just the names of the - books, and make the assertion pass. - @function: `firstTenBooks` - @input params: None - @output: [{id, title, author_id, subject_id}] -*/ -let firstTenBooks; // = .... IMPLEMENT THIS FUNCTION +let firstTenBooks = db.any('SELECT * FROM books LIMIT 10'); // = .... IMPLEMENT THIS FUNCTION firstTenBooks.then(books => { assert(books.length, 10) }).catch(error => { @@ -67,23 +67,23 @@ firstTenBooks.then(books => { -/* ----------------------------------------- - Exercise 3 - ----------------------------------------- + // ----------------------------------------- + // Exercise 3 + // ----------------------------------------- + + // Implement the function `findAuthorsOrderedByLastName` which returns all the + // authors from the the `authors` table, and the rows are ordered by the + // `last_name`. - Implement the function `findAuthorsOrderedByLastName` which returns all the - authors from the the `authors` table, and the rows are ordered by the - `last_name`. + // @function: `findAuthorsOrderedByLastName` + // @input params: None + // @output: [{id, first_name, last_name}] - @function: `findAuthorsOrderedByLastName` - @input params: None - @output: [{id, first_name, last_name}] -*/ -let findAuthorsOrderedByLastName; // = .... IMPLEMENT THIS FUNCTION +let findAuthorsOrderedByLastName = db.any('SELECT * FROM authors ORDER BY last_name'); // = .... IMPLEMENT THIS FUNCTION findAuthorsOrderedByLastName.then(authors => { assert.deepEqual(authors.length, 19) assert.deepEqual(authors[0].last_name, 'Alcott') @@ -91,72 +91,97 @@ findAuthorsOrderedByLastName.then(authors => { }).catch(error => { console.log('Whoops, my function doesnt behave as expected.', error); }); - +// /* --------End of Exercise 3---------------- */ -/* ----------------------------------------- - Exercise 4 - ----------------------------------------- - - Implement the function `findBookAuthors` which returns the `first_name` and - `last_name` from the `authors` table, and the `title` of the - books(from the `books` table) that the authors have written. - - @function: `findBookAuthors` - @input params: None - @output: [{first_name, last_name, title}] - - In this exercise you will ALSO have to write the assertions. For inspiration, - look at the assertions in Exercises 1 - 3. - - Expected Result: - [{first_name: 'John', last_name: 'Worsley', title: 'Practical PostgreSQL'} - {first_name: 'Paulette', last_name: 'Bourgeois', title: 'Franklin in the Dark'} - {first_name: 'Margery Williams', last_name: 'Bianco', title: 'The Velveteen Rabbit'} - {first_name: 'Louisa May', last_name: 'Alcott', title: 'Little Women'} - {first_name: 'Stephen', last_name: 'King', title: 'The Shining'} - {first_name: 'Frank', last_name: 'Herbert', title: 'Dune'} - {first_name: 'Burne', last_name: 'Hogarth', title: 'Dynamic Anatomy'} - {first_name: 'Margaret Wise', last_name: 'Brown', title: 'Goodnight Moon'} - {first_name: 'Edgar Allen', last_name: 'Poe', title: 'The Tell-Tale Heart'} - {first_name: 'Mark', last_name: 'Lutz', title: 'Learning Python'} - {first_name: 'Mark', last_name: 'Lutz', title: 'Programming Python'} - {first_name: 'Tom', last_name: 'Christiansen', title: 'Perl Cookbook'} - {first_name: 'Arthur C.', last_name: 'Clarke', title: '2001: A Space Odyssey'} - {first_name: 'Theodor Seuss', last_name: 'Geisel', title: 'Bartholomew and the Oobleck'} - {first_name: 'Theodor Seuss', last_name: 'Geisel', title: 'The Cat in the Hat'}] -*/ -let findBookAuthors; // IMPLEMENT THIS FUNCTION - + // ----------------------------------------- + // Exercise 4 + // ----------------------------------------- + + // Implement the function `findBookAuthors` which returns the `first_name` and + // `last_name` from the `authors` table, and the `title` of the + // books(from the `books` table) that the authors have written. + + // @function: `findBookAuthors` + // @input params: None + // @output: [{first_name, last_name, title}] + + // In this exercise you will ALSO have to write the assertions. For inspiration, + // look at the assertions in Exercises 1 - 3. + + // Expected Result: + // [{first_name: 'John', last_name: 'Worsley', title: 'Practical PostgreSQL'} + // {first_name: 'Paulette', last_name: 'Bourgeois', title: 'Franklin in the Dark'} + // {first_name: 'Margery Williams', last_name: 'Bianco', title: 'The Velveteen Rabbit'} + // {first_name: 'Louisa May', last_name: 'Alcott', title: 'Little Women'} + // {first_name: 'Stephen', last_name: 'King', title: 'The Shining'} + // {first_name: 'Frank', last_name: 'Herbert', title: 'Dune'} + // {first_name: 'Burne', last_name: 'Hogarth', title: 'Dynamic Anatomy'} + // {first_name: 'Margaret Wise', last_name: 'Brown', title: 'Goodnight Moon'} + // {first_name: 'Edgar Allen', last_name: 'Poe', title: 'The Tell-Tale Heart'} + // {first_name: 'Mark', last_name: 'Lutz', title: 'Learning Python'} + // {first_name: 'Mark', last_name: 'Lutz', title: 'Programming Python'} + // {first_name: 'Tom', last_name: 'Christiansen', title: 'Perl Cookbook'} + // {first_name: 'Arthur C.', last_name: 'Clarke', title: '2001: A Space Odyssey'} + // {first_name: 'Theodor Seuss', last_name: 'Geisel', title: 'Bartholomew and the Oobleck'} + // {first_name: 'Theodor Seuss', last_name: 'Geisel', title: 'The Cat in the Hat'}] + + +let findBookAuthors = db.any( + SELECT * + FROM authors a + JOIN books b + ON a.id = b.id + ); + +findBookAuthors.then(function bookAuthors(){ + assert.deepEqual(books.authors.length, 15) + assert.deepEqual(authors[0].last_name, 'Alcott') + assert.deepEqual(authors[1].first_name, 'Paulette') +}).catch(error => { + console.log('Whoops, my joint is bugging out, b', error); +}); /* --------End of Exercise 4---------------- */ -/* ----------------------------------------- - Exercise 5 - ----------------------------------------- + // ----------------------------------------- + // Exercise 5 + // ----------------------------------------- + + // Implement the function `authorIdWithTwoBooks` which returns the + // `author_id` of authors who have 2 books. (HINT: you have to use a SUBQUERY) - Implement the function `authorIdWithTwoBooks` which returns the - `author_id` of authors who have 2 books. (HINT: you have to use a SUBQUERY) + // @function: `authorIdWithTwoBooks` + // @input params: None + // @output: [{first_name, last_name, title}] - @function: `authorIdWithTwoBooks` - @input params: None - @output: [{first_name, last_name, title}] + // In this exercise you will ALSO have to write the assertions. For inspiration, + // look at the assertions in Exercises 1 - 3. - In this exercise you will ALSO have to write the assertions. For inspiration, - look at the assertions in Exercises 1 - 3. + // Expected Result: + // [{author_id: 1809}, + // {author_id: 7805}] - Expected Result: - [{author_id: 1809}, - {author_id: 7805}] -*/ -let authorIdWithTwoBooks; // IMPLEMENT THIS FUNCTION +let authorIdWithTwoBooks= db.any(SELECT twoBooks.author_id FROM + (SELECT author_id, count(author_id) AS total FROM authors + JOIN books + ON authors.id = books.author_id + GROUP BY author_id) twoBooks WHERE total = 2); + +authorIdWithTwoBooks.then(function twoBookId(){ + assert.deepEqual(twoBookId.length, 2) + assert.deepEqual(twoBookId[0].authors.id, 1809) + assert.deepEqual(twoBookId[1].authors.id, 7805) +}).catch(error => { + console.log('Whoops, my joint is bugging out, b', error); +}); /* --------End of Exercise 5---------------- */ @@ -164,56 +189,86 @@ let authorIdWithTwoBooks; // IMPLEMENT THIS FUNCTION -/* ----------------------------------------- - Exercise 6 - ----------------------------------------- + // ----------------------------------------- + // Exercise 6 + // ----------------------------------------- + + // Implement the function `bookTitlesWithMultipleEditions` which returns the + // `title` of books which have more than one entry in the editions table (do not use the "edition" field). (HINT: you have to use a join) + + // @function: `bookTitlesWithMultipleEditions` + // @input params: None + // @output: [{title}] - Implement the function `bookTitlesWithMultipleEditions` which returns the - `title` of books which have more than one entry in the editions table (do not use the "edition" field). (HINT: you have to use a join) + // In this exercise you will ALSO have to write the assertions. For inspiration, + // look at the assertions in Exercises 1 - 3. - @function: `bookTitlesWithMultipleEditions` - @input params: None - @output: [{title}] + // Expected Result: + // [{title: 'The Shining'}, + // {title: 'The Cat in the Hat'}, + // {title: 'Dune'} + // {title: '2001: A Space Odyssey'} + // {title: 'The Tell-Tale Heart'}] - In this exercise you will ALSO have to write the assertions. For inspiration, - look at the assertions in Exercises 1 - 3. - Expected Result: - [{title: 'The Shining'}, - {title: 'The Cat in the Hat'}, - {title: 'Dune'} - {title: '2001: A Space Odyssey'} - {title: 'The Tell-Tale Heart'}] +let bookTitlesWithMultipleEditions = db.any(SELECT author_name +FROM books +GROUP BY author_name +HAVING COUNT(distinct page_count) > 1 +); -*/ -let bookTitlesWithMultipleEditions; // IMPLEMENT THIS FUNCTION +bookTitlesWithMultipleEditions.then( function multipleEditions(){ + assert.deepEqual(multipleEditions.length, 5) + assert.deepEqual(multipleEditions[0].title, 'The Shining') + assert.deepEqual(multipleEditions[1].title, 'The Cat in the Hat') + +}) /* --------End of Exercise 6---------------- */ -/* ----------------------------------------- - Exercise 7 - ----------------------------------------- + // ----------------------------------------- + // Exercise 7 + // ----------------------------------------- + + // Implement the function `findStockedBooks` which returns the `title` & the + // author's `first_name` & `last_name` of all books which are stocked as + // represented in the `daily_inventory` table. + + // @function: `findStockedBooks` + // @input params: None + // @output: [{first_name, last_name, title}] + + // In this exercise you will ALSO have to write the assertions. For inspiration, + // look at the assertions in Exercises 1 - 3. - Implement the function `findStockedBooks` which returns the `title` & the - author's `first_name` & `last_name` of all books which are stocked as - represented in the `daily_inventory` table. + // Expected Result: + // [ {first_name: 'Frank', title: 'Dune', last_name: 'Herbert'}, + // {title: 'The Cat in the Hat', first_name: 'Theodor Seuss', last_name: 'Geisel'}] - @function: `findStockedBooks` - @input params: None - @output: [{first_name, last_name, title}] - In this exercise you will ALSO have to write the assertions. For inspiration, - look at the assertions in Exercises 1 - 3. + let findStockedBooks = db.any(SELECT title, first_name, last_name, is_stocked + FROM daily_inventory d + JOIN editions e + ON d.isbn = e.isbn + JOIN books b + ON b.id = e.book_id + JOIN authors a + ON a.id = b.author_id + +WHERE is_stocked = TRUE + +); - Expected Result: - [ {first_name: 'Frank', title: 'Dune', last_name: 'Herbert'}, - {title: 'The Cat in the Hat', first_name: 'Theodor Seuss', last_name: 'Geisel'}] +findStockedBooks.then(function(){ + assert.deepEqual(multipleEditions.length, 3) + assert.deepEqual(multipleEditions[0].title, 'Dune') + assert.deepEqual(multipleEditions[0].last_name, 'Herbert') + assert.deepEqual(multipleEditions[2].title, 'The Cat in the Hat') -*/ -let findStockedBooks; // IMPLEMENT THIS FUNCTION +}) /* --------End of Exercise 7---------------- */ From 3556858e145abe99703c5da9a9857e25ae27c28b Mon Sep 17 00:00:00 2001 From: waynebanks Date: Wed, 19 Apr 2017 11:58:42 -0700 Subject: [PATCH 02/17] completed warmup of day4 ex1 --- promise-it-wont-hurt-solutions/src/01-warmup.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/promise-it-wont-hurt-solutions/src/01-warmup.js b/promise-it-wont-hurt-solutions/src/01-warmup.js index e69de29..70eb5d0 100644 --- a/promise-it-wont-hurt-solutions/src/01-warmup.js +++ b/promise-it-wont-hurt-solutions/src/01-warmup.js @@ -0,0 +1,7 @@ +// Using setTimeout, print the string 'TIMED OUT!' after 300ms. + +function timeFunc () { + console.log('TIMED OUT'); +} + +setTimeout(timeFunc, 3000); From 44e411654d963fbef836ac9f2365196f73f1913a Mon Sep 17 00:00:00 2001 From: waynebanks Date: Wed, 19 Apr 2017 15:04:36 -0700 Subject: [PATCH 03/17] completed ex 2 of 13 for day 4 exercises --- .../src/01-warmup.js | 3 +- .../src/02-fullfill-a-promise.js | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/promise-it-wont-hurt-solutions/src/01-warmup.js b/promise-it-wont-hurt-solutions/src/01-warmup.js index 70eb5d0..8a24f62 100644 --- a/promise-it-wont-hurt-solutions/src/01-warmup.js +++ b/promise-it-wont-hurt-solutions/src/01-warmup.js @@ -1,7 +1,8 @@ // Using setTimeout, print the string 'TIMED OUT!' after 300ms. +// promise-it-wont-hurt run promise-it-wont-hurt-solutions/src/01-warmup.js function timeFunc () { - console.log('TIMED OUT'); + console.log('TIMED OUT!'); } setTimeout(timeFunc, 3000); diff --git a/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js b/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js index e69de29..1aa780d 100644 --- a/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js +++ b/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js @@ -0,0 +1,28 @@ +'use strict'; + + var promise = new Promise(function (fulfill, reject) { + // Your solution here + setTimeout(function(){ + fulfill('FULFILLED!'); + }) + + }, 3000); + promise.then((successMessage) => { + console.log(successMessage); + }); + + + + + // Your solution here + +// es6Promise = require('es6-promise'); +// 'use strict'; + + +// var promise = new Promise(function (fulfill, reject) { +// setTimeout(function(){ +// fulfill('FULFILLED!'); +// }) + +// }, 300); \ No newline at end of file From 45e0c2b00086aa62aa2734f5e12bf30edb10426f Mon Sep 17 00:00:00 2001 From: waynebanks Date: Wed, 19 Apr 2017 16:21:02 -0700 Subject: [PATCH 04/17] completed ex 3 of 13 for day 4 exercises --- .../src/02-fullfill-a-promise.js | 6 +++--- .../src/03-reject-a-promise.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js b/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js index 1aa780d..fe66f94 100644 --- a/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js +++ b/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js @@ -1,6 +1,4 @@ -'use strict'; - - var promise = new Promise(function (fulfill, reject) { + var promise = new Promise(function (fulfill, reject) { // Your solution here setTimeout(function(){ fulfill('FULFILLED!'); @@ -11,6 +9,8 @@ console.log(successMessage); }); + + diff --git a/promise-it-wont-hurt-solutions/src/03-reject-a-promise.js b/promise-it-wont-hurt-solutions/src/03-reject-a-promise.js index e69de29..d7c5b4b 100644 --- a/promise-it-wont-hurt-solutions/src/03-reject-a-promise.js +++ b/promise-it-wont-hurt-solutions/src/03-reject-a-promise.js @@ -0,0 +1,16 @@ + + + var promise = new Promise(function (fulfill, reject) { + // Your solution here + // function onReject(error) { + setTimeout(function(){ + fulfill('REJECTED!'); + }, 3000) + }); + + + + promise.then((failureMessage) => { + console.log(failureMessage); + }) + \ No newline at end of file From 97a34555a9f484cd4d4f970833a9319946c561d1 Mon Sep 17 00:00:00 2001 From: waynebanks Date: Wed, 19 Apr 2017 19:34:58 -0700 Subject: [PATCH 05/17] completed ex 4 of 13 for day 4 exercises --- .../src/02-fullfill-a-promise.js | 2 +- .../src/03-reject-a-promise.js | 19 ++++++++++--------- .../src/04-to-reject-or-not-to-reject.js | 11 +++++++++++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js b/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js index fe66f94..e2afd59 100644 --- a/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js +++ b/promise-it-wont-hurt-solutions/src/02-fullfill-a-promise.js @@ -6,7 +6,7 @@ }, 3000); promise.then((successMessage) => { - console.log(successMessage); + console.log(); }); diff --git a/promise-it-wont-hurt-solutions/src/03-reject-a-promise.js b/promise-it-wont-hurt-solutions/src/03-reject-a-promise.js index d7c5b4b..baa8754 100644 --- a/promise-it-wont-hurt-solutions/src/03-reject-a-promise.js +++ b/promise-it-wont-hurt-solutions/src/03-reject-a-promise.js @@ -2,15 +2,16 @@ var promise = new Promise(function (fulfill, reject) { // Your solution here - // function onReject(error) { - setTimeout(function(){ - fulfill('REJECTED!'); - }, 3000) - }); + function onReject (error) { + setTimeout(function(){ + fulfill('REJECTED!'); + }, 3000) + + } - - promise.then((failureMessage) => { - console.log(failureMessage); - }) + promise.then((failureMessage) => { + console.log(failureMessage); + }); + }); \ No newline at end of file diff --git a/promise-it-wont-hurt-solutions/src/04-to-reject-or-not-to-reject.js b/promise-it-wont-hurt-solutions/src/04-to-reject-or-not-to-reject.js index e69de29..c854cef 100644 --- a/promise-it-wont-hurt-solutions/src/04-to-reject-or-not-to-reject.js +++ b/promise-it-wont-hurt-solutions/src/04-to-reject-or-not-to-reject.js @@ -0,0 +1,11 @@ + var promise = new Promise(function (resolve, reject) { + // Your solution here + + resolve('I FIRED!'); + + }) + promise.then((successMessage) => { + console.log(successMessage); + }).catch((failureMessage) => { + console.log('I DID NOT FIRE'); + }) \ No newline at end of file From 471920116bb4dfda9a14e7a3bf621a02db685171 Mon Sep 17 00:00:00 2001 From: waynebanks Date: Wed, 19 Apr 2017 20:03:48 -0700 Subject: [PATCH 06/17] completed ex 5 of 13 for day 4 exercises --- .../src/05-always-asynchronous.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/promise-it-wont-hurt-solutions/src/05-always-asynchronous.js b/promise-it-wont-hurt-solutions/src/05-always-asynchronous.js index e69de29..8202de3 100644 --- a/promise-it-wont-hurt-solutions/src/05-always-asynchronous.js +++ b/promise-it-wont-hurt-solutions/src/05-always-asynchronous.js @@ -0,0 +1,12 @@ + var promise = new Promise(function (resolve, reject) { + // Your solution here + + resolve('PROMISE VALUE'); + + }) + promise.then((successMessage) => { + console.log(successMessage); + }).catch((failureMessage) => { + console.log('I DID NOT FIRE'); + }) +console.log('MAIN PROGRAM'); \ No newline at end of file From 05d3909162b15993314a772ecd539bcbb2a8964d Mon Sep 17 00:00:00 2001 From: waynebanks Date: Wed, 19 Apr 2017 20:15:54 -0700 Subject: [PATCH 07/17] completed ex 6 of 13 for day 4 exercises --- promise-it-wont-hurt-solutions/src/06-shortcuts.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/promise-it-wont-hurt-solutions/src/06-shortcuts.js b/promise-it-wont-hurt-solutions/src/06-shortcuts.js index e69de29..8202de3 100644 --- a/promise-it-wont-hurt-solutions/src/06-shortcuts.js +++ b/promise-it-wont-hurt-solutions/src/06-shortcuts.js @@ -0,0 +1,12 @@ + var promise = new Promise(function (resolve, reject) { + // Your solution here + + resolve('PROMISE VALUE'); + + }) + promise.then((successMessage) => { + console.log(successMessage); + }).catch((failureMessage) => { + console.log('I DID NOT FIRE'); + }) +console.log('MAIN PROGRAM'); \ No newline at end of file From 06ccdb8be6e32746adbd03bc48b6dc40b9273e5b Mon Sep 17 00:00:00 2001 From: waynebanks Date: Thu, 20 Apr 2017 10:32:52 -0700 Subject: [PATCH 08/17] completed ex 7 of 13 for day 4 exercises --- .../src/07-promise-after-promise.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/promise-it-wont-hurt-solutions/src/07-promise-after-promise.js b/promise-it-wont-hurt-solutions/src/07-promise-after-promise.js index e69de29..0a049bb 100644 --- a/promise-it-wont-hurt-solutions/src/07-promise-after-promise.js +++ b/promise-it-wont-hurt-solutions/src/07-promise-after-promise.js @@ -0,0 +1,45 @@ +// Parse.User.logIn('user', 'pass').then(function (user) { +// return query.find(); +// }).then(function (results) { +// return results[0].save({ key: value }); +// }).then(function (result) { +// // the object was saved +// }); + +// function timeFunc() { +// console.log('TIMED OUT!'); +// } + +// setTimeout(timeFunc, 3000); + +function first(){ + return new Promise(resolve =>{ + setTimeout(() =>{ + resolve("the first one...") + }, 1000) + }); +} +function second(){ + return new Promise(resolve =>{ + setTimeout(() =>{ + resolve("the second one...") + }, 1000) + }); +} +function third(){ + return new Promise(resolve =>{ + setTimeout(() =>{ + resolve("the third one...") + }, 1000) + }); +} + +first().then(second =>{ + console.log(second) +}) +second().then(third =>{ + console.log(third) +}) +third().then(fourth =>{ + console.log('done and done...') +}) \ No newline at end of file From f25a6ddfd28c8f8e554c0ffbdb533b6d4348b001 Mon Sep 17 00:00:00 2001 From: waynebanks Date: Thu, 20 Apr 2017 10:58:41 -0700 Subject: [PATCH 09/17] completed ex 8 of 13 for day 4 exercises --- .../src/07-promise-after-promise.js | 2 +- .../src/08-values-and-promises.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/promise-it-wont-hurt-solutions/src/07-promise-after-promise.js b/promise-it-wont-hurt-solutions/src/07-promise-after-promise.js index 0a049bb..99626bb 100644 --- a/promise-it-wont-hurt-solutions/src/07-promise-after-promise.js +++ b/promise-it-wont-hurt-solutions/src/07-promise-after-promise.js @@ -40,6 +40,6 @@ first().then(second =>{ second().then(third =>{ console.log(third) }) -third().then(fourth =>{ +third().then(function (){ console.log('done and done...') }) \ No newline at end of file diff --git a/promise-it-wont-hurt-solutions/src/08-values-and-promises.js b/promise-it-wont-hurt-solutions/src/08-values-and-promises.js index e69de29..0b51bfd 100644 --- a/promise-it-wont-hurt-solutions/src/08-values-and-promises.js +++ b/promise-it-wont-hurt-solutions/src/08-values-and-promises.js @@ -0,0 +1,7 @@ +function attachTitle(cb) { + return 'DR.'+ ' ' + cb +} + +Promise.resolve('MANHATTAN') +.then(attachTitle) +.then(console.log) \ No newline at end of file From 351963eca2e275b0033eb18b02a38f48a35692ad Mon Sep 17 00:00:00 2001 From: waynebanks Date: Thu, 20 Apr 2017 15:03:59 -0700 Subject: [PATCH 10/17] completed ex 9 of 13 for day 4 exercises --- .../src/09-throw-an-error.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/promise-it-wont-hurt-solutions/src/09-throw-an-error.js b/promise-it-wont-hurt-solutions/src/09-throw-an-error.js index e69de29..0319a07 100644 --- a/promise-it-wont-hurt-solutions/src/09-throw-an-error.js +++ b/promise-it-wont-hurt-solutions/src/09-throw-an-error.js @@ -0,0 +1,28 @@ +function parsePromised (inputString) { + return new Promise( function (resolve, reject) { + // Your solution here + try { + JSON.parse(inputString) + resolve('PROMISE VALUE'); + } + catch (err) { + console.log('an error occurred -->', err) + reject(err) + } + + }) +} +parsePromised('a') +.then( function (resolveValue) { + console.log('the promise resolved and returned -->', resolveValue) +}) +.catch( function (rejectValue) { + console.log('the promise was rejected and returned -->', rejectValue) +}) +parsePromised('{"a":"b"}') +.then( function (resolveValue) { + console.log('the promise resolved and returned -->', resolveValue) +}) +.catch( function (rejectValue) { + console.log('the promise was rejected and returned -->', rejectValue) +}) \ No newline at end of file From 32ec717629e376c8463f81d4c610abe80de9cb6c Mon Sep 17 00:00:00 2001 From: waynebanks Date: Thu, 20 Apr 2017 15:49:25 -0700 Subject: [PATCH 11/17] completed ex 10 of 13 for day 4 exercises --- .../src/10-an-important-file.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/promise-it-wont-hurt-solutions/src/10-an-important-file.js b/promise-it-wont-hurt-solutions/src/10-an-important-file.js index e69de29..395c065 100644 --- a/promise-it-wont-hurt-solutions/src/10-an-important-file.js +++ b/promise-it-wont-hurt-solutions/src/10-an-important-file.js @@ -0,0 +1,20 @@ +function alwaysThrows() { + throw new Error( 'OH NOES' ) +} + +function iterate(num) { + alert(num) + return num + 1 +} +function drop(num) { + alert(num) + return num - 3 +} + +Promise.resolve(iterate(1)) +.then(iterate) +.then(iterate) +.then(drop) +.then(iterate).then(iterate).then(iterate).then(iterate).then(iterate) +.then(alwaysThrows) +.catch(console.log) \ No newline at end of file From 4c5e8995c075d290e39fc65535edd0a43664d9e8 Mon Sep 17 00:00:00 2001 From: waynebanks Date: Thu, 20 Apr 2017 20:19:45 -0700 Subject: [PATCH 12/17] completed ex 11 of 13 for day 4 exercises; solutions do not pass --- .../src/11-multiple-promises.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/promise-it-wont-hurt-solutions/src/11-multiple-promises.js b/promise-it-wont-hurt-solutions/src/11-multiple-promises.js index e69de29..cfa2a70 100644 --- a/promise-it-wont-hurt-solutions/src/11-multiple-promises.js +++ b/promise-it-wont-hurt-solutions/src/11-multiple-promises.js @@ -0,0 +1,25 @@ +function all(promise1, promise2) { + this.counter = 0 + let promises = [] + + function count() { + counter++ + if (counter === 2) resolve(promises) + } + + return new Promise((resolve, reject) => { + + + promise1 + .then((val) => { + // promises[0] = val + counter ++ + }) +console.log(counter) + promise2 + .then((val) => { + promises[1] = val + count() + }) + }) +} \ No newline at end of file From 57ca64b99768f229e8a53abaaf3bc1d5e96b44dc Mon Sep 17 00:00:00 2001 From: waynebanks Date: Fri, 21 Apr 2017 09:45:21 -0700 Subject: [PATCH 13/17] completed ex 12 of 13 for day 4 exercises --- promise-it-wont-hurt-solutions/src/12-fetch-json.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/promise-it-wont-hurt-solutions/src/12-fetch-json.js b/promise-it-wont-hurt-solutions/src/12-fetch-json.js index e69de29..1ee1c46 100644 --- a/promise-it-wont-hurt-solutions/src/12-fetch-json.js +++ b/promise-it-wont-hurt-solutions/src/12-fetch-json.js @@ -0,0 +1,8 @@ +var HTTP = require("q-io/http"); + + +HTTP.read('http://localhost:1337').then(function(result){ + console.log(JSON.parse(result)); +}); + + From 4332c7ee4fb4ff37a359669656cc384ee7113d72 Mon Sep 17 00:00:00 2001 From: waynebanks Date: Fri, 21 Apr 2017 10:19:40 -0700 Subject: [PATCH 14/17] completed ex 13 of 13 for day 4 exercises --- .../src/11-multiple-promises.js | 82 ++++++++++++++----- .../src/13-do-some-work.js | 25 ++++++ 2 files changed, 86 insertions(+), 21 deletions(-) diff --git a/promise-it-wont-hurt-solutions/src/11-multiple-promises.js b/promise-it-wont-hurt-solutions/src/11-multiple-promises.js index cfa2a70..bcb6914 100644 --- a/promise-it-wont-hurt-solutions/src/11-multiple-promises.js +++ b/promise-it-wont-hurt-solutions/src/11-multiple-promises.js @@ -1,25 +1,65 @@ -function all(promise1, promise2) { - this.counter = 0 - let promises = [] +// function all(promises) { +// console.log(promises) +// let counter = 0 +// // let promises = [] - function count() { - counter++ - if (counter === 2) resolve(promises) - } +// function count() { +// // counter++ +// if (counter === 2) resolve(promises) +// } - return new Promise((resolve, reject) => { +// return new Promise((resolve, reject) => { - promise1 - .then((val) => { - // promises[0] = val - counter ++ - }) -console.log(counter) - promise2 - .then((val) => { - promises[1] = val - count() - }) - }) -} \ No newline at end of file +// promises[0] +// .then((val) => { +// // promises[0] = val +// counter ++ +// }) + +// console.log(counter) + +// promises[1] +// .then((val) => { +// // promises[1] = val +// counter++ +// }) +// }) + +// count() +// } + +function all(para1, para2){ + + return new Promise(function(resolve, reject){ + + var counter = 0; + var promiseArr = []; + + para1.then(function(result){ + promiseArr.push(result); + counter ++; + if(counter === 2){ + resolve(promiseArr); + } + }); + + para2.then(function(result){ + promiseArr.push(result); + counter ++; + if(counter === 2){ + resolve(promiseArr); + } + }); + + + }); + + + } + + + + + +all(getPromise1(), getPromise2()).then(console.log); \ No newline at end of file diff --git a/promise-it-wont-hurt-solutions/src/13-do-some-work.js b/promise-it-wont-hurt-solutions/src/13-do-some-work.js index e69de29..2be3bed 100644 --- a/promise-it-wont-hurt-solutions/src/13-do-some-work.js +++ b/promise-it-wont-hurt-solutions/src/13-do-some-work.js @@ -0,0 +1,25 @@ +/*Let's talk to two remote processes over HTTP being run by your friend +and mine, "localhost". + + * Port 7000: Faux session cache (Redis or some such thing) + * Port 7001: Faux database (MongoDB, LevelDB, Postgres, etc) + +As in the previous lesson, use the q-io module to create promises +as wrappers for HTTP responses. Hint: You will probably need more +than one promise… + + * Send an HTTP GET request to the session cache on port 7000. A stringwill be returned to you representing a user ID. + * Grab that ID from the session response and send an HTTP GET request toyour database on port 7001 to the url `localhost:7001/`. + * If successfully done, your database will return a user object.`console.log` it to win many nerd-points. + */ +var HTTP = require("q-io/http"); + + +HTTP.read('http://localhost:7000') +.then(function(result){ + return HTTP.read('http://localhost:7001' + result) +}) + .then(function(result){console.log(JSON.parse(result)); + +}) +.then(null, console.log); \ No newline at end of file From e2d4734553f3533fc168a4aeb458669b9e0d25fa Mon Sep 17 00:00:00 2001 From: waynebanks Date: Fri, 21 Apr 2017 10:22:18 -0700 Subject: [PATCH 15/17] completed ex 13 of 13 for day 4 exercises --- .../src/13-do-some-work.js | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/promise-it-wont-hurt-solutions/src/13-do-some-work.js b/promise-it-wont-hurt-solutions/src/13-do-some-work.js index 2be3bed..c582241 100644 --- a/promise-it-wont-hurt-solutions/src/13-do-some-work.js +++ b/promise-it-wont-hurt-solutions/src/13-do-some-work.js @@ -1,25 +1,35 @@ -/*Let's talk to two remote processes over HTTP being run by your friend -and mine, "localhost". +// Let's talk to two remote processes over HTTP being run by your friend +// and mine, "localhost". - * Port 7000: Faux session cache (Redis or some such thing) - * Port 7001: Faux database (MongoDB, LevelDB, Postgres, etc) +// * Port 7000: Faux session cache (Redis or some such thing) +// * Port 7001: Faux database (MongoDB, LevelDB, Postgres, etc) -As in the previous lesson, use the q-io module to create promises -as wrappers for HTTP responses. Hint: You will probably need more -than one promise… +// As in the previous lesson, use the q-io module to create promises +// as wrappers for HTTP responses. Hint: You will probably need more +// than one promise… - * Send an HTTP GET request to the session cache on port 7000. A stringwill be returned to you representing a user ID. - * Grab that ID from the session response and send an HTTP GET request toyour database on port 7001 to the url `localhost:7001/`. - * If successfully done, your database will return a user object.`console.log` it to win many nerd-points. - */ -var HTTP = require("q-io/http"); +// * Send an HTTP GET request to the session cache on port 7000. A stringwill be returned to you representing a user ID. +// * Grab that ID from the session response and send an HTTP GET request toyour database on port 7001 to the url `localhost:7001/`. +// * If successfully done, your database will return a user object.`console.log` it to win many nerd-points. + +// var HTTP = require("q-io/http"); -HTTP.read('http://localhost:7000') -.then(function(result){ - return HTTP.read('http://localhost:7001' + result) -}) - .then(function(result){console.log(JSON.parse(result)); +// HTTP.read('http://localhost:7000') +// .then(function(result){ +// return HTTP.read('http://localhost:7001' + result) +// }) +// .then(function(result){console.log(JSON.parse(result)); -}) -.then(null, console.log); \ No newline at end of file +// }) +// .then(null, console.log); + +let HTTP = require( 'q-io/http' ) + +HTTP.read( "http://localhost:7000" ) + .then(function(id) { + return HTTP.read( "http://localhost:7001/" + id ) + }) + .then(JSON.parse) + .then(console.log) + .done() \ No newline at end of file From a1bb76cc0e527b13db3b098f774b69fb6ad0c59a Mon Sep 17 00:00:00 2001 From: waynebanks Date: Fri, 21 Apr 2017 10:48:35 -0700 Subject: [PATCH 16/17] updated the readme --- promise-it-wont-hurt-solutions/README.md | 93 ++++++++++-------------- 1 file changed, 40 insertions(+), 53 deletions(-) diff --git a/promise-it-wont-hurt-solutions/README.md b/promise-it-wont-hurt-solutions/README.md index e03f7a0..05e7d9b 100644 --- a/promise-it-wont-hurt-solutions/README.md +++ b/promise-it-wont-hurt-solutions/README.md @@ -1,54 +1,41 @@ -# Skeleton Repo for the solutions for the awesome [tutorial](https://github.com/stevekane/promise-it-wont-hurt) on Promises. -This repo contains all the source files for the solutions of the `promise it won't hurt` exercises. The tutorial is meant to give you a hands on understanding of how to use promises. - - -## Getting Started - -#### Install the package `promise-it-wont-hurt` which contains the tutorials -Run this in a terminal window : -```bash -npm install -g promise-it-wont-hurt@latest -``` -## Exercises - -### Usage Instructions - -#### 1. Selecting a problem to work on - -Once the workshop is installed, run `promise-it-wont-hurt` to print a menu -where you can select a problem to work on. - -Run this command in a terminal window -```bash -promise-it-wont-hurt -``` - -Problems are listed in rough order of difficulty. You are advised to complete them in order, as later problems -will build on skills developed by solving previous problems. - -#### 2. Writing your solution - -Once you have selected a problem, the workshop will remember which problem you are working on. -Using your preferred editor, simply edit the corresponding file inside the `src` folder to write your solution in. - -#### 3. Testing your solution - -Use the workshop's `run` command to point the workshop at your solution file. Your solution will loaded -and passed the problem input. This usually won't perform any validation, it will only show the program output. - -Run this command in a terminal window -```bash -promise-it-wont-hurt run src/01-warmup.js -``` - -#### 4. Verifying your solution - -Your solution will be verified against the output of the 'official' solution. -If all of the output matches, then you have successfully solved the problem! - -Run this command in a terminal window -```bash -promise-it-wont-hurt verify src/01-warmup.js -``` - +#Specifications + +##Day 1 & 2 + + [x]Complete these sections (including the exercises) of the Rethinking Asynchronous JavaScript on Frontend Masters: + []Parallel vs. Async + []Callback + []Thunks + []Promises + []Complete the Understanding Promises in JavaScript workshop (22m) + []Read this blog post + []Read this blog post +If you complete the above with time to spare, fill any gaps in your understanding by skimming through the Promises Course on Udacity. + +Day 3 + + []Look at the documentation of the pg-promise repository + []Exercises 1-7 in the pg promise exercises are complete + []Exercise 1 + []Exercise 2 + []Exercise 3 + []Exercise 4 + []Exercise 5 + []Exercise 6 + []Exercise 7 +Day 4 & 5 + + []Exercises 1-13 in the promise-it-wont-hurt-solutions are complete and written to the appropriate file: + []src/01-warmup.js + []src/02-fullfill-a-promise.js + []src/03-reject-a-promise.js + []src/04-to-reject-or-not-to-reject.js + []src/05-always-asynchronous.js + []src/06-shortcuts.js + []src/07-promise-after-promise.js + []src/08-values-and-promises.js []src/09-throw-an-error.js * see note below + []src/10-an-important-file.js * see note below + []src/11-multiple-promises.js + []src/12-fetch-json.js + []src/13-do-some-work.js From 92d9cfd451189565f6836732e363cdfb93dd9c32 Mon Sep 17 00:00:00 2001 From: waynebanks Date: Fri, 21 Apr 2017 14:13:10 -0700 Subject: [PATCH 17/17] completed ex 13 of 13 for day 4 exercises --- promise-it-wont-hurt-solutions/README.md | 64 ++++++++++++------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/promise-it-wont-hurt-solutions/README.md b/promise-it-wont-hurt-solutions/README.md index 05e7d9b..a2f823f 100644 --- a/promise-it-wont-hurt-solutions/README.md +++ b/promise-it-wont-hurt-solutions/README.md @@ -1,41 +1,41 @@ -#Specifications +# Specifications -##Day 1 & 2 +## Day 1 & 2 - [x]Complete these sections (including the exercises) of the Rethinking Asynchronous JavaScript on Frontend Masters: - []Parallel vs. Async - []Callback - []Thunks - []Promises - []Complete the Understanding Promises in JavaScript workshop (22m) - []Read this blog post - []Read this blog post + [x] Complete these sections (including the exercises) of the Rethinking Asynchronous JavaScript on Frontend Masters: + [x]Parallel vs. Async + [x]Callback + [x]Thunks + [x]Promises + [x]Complete the Understanding Promises in JavaScript workshop (22m) + [x]Read this blog post + [x]Read this blog post If you complete the above with time to spare, fill any gaps in your understanding by skimming through the Promises Course on Udacity. Day 3 - []Look at the documentation of the pg-promise repository - []Exercises 1-7 in the pg promise exercises are complete - []Exercise 1 - []Exercise 2 - []Exercise 3 - []Exercise 4 - []Exercise 5 - []Exercise 6 - []Exercise 7 + [x]Look at the documentation of the pg-promise repository + [x]Exercises 1-7 in the pg promise exercises are complete + [x]Exercise 1 + [x]Exercise 2 + [x]Exercise 3 + [x]Exercise 4 + [x]Exercise 5 + [x]Exercise 6 + [x]Exercise 7 Day 4 & 5 - []Exercises 1-13 in the promise-it-wont-hurt-solutions are complete and written to the appropriate file: - []src/01-warmup.js - []src/02-fullfill-a-promise.js - []src/03-reject-a-promise.js - []src/04-to-reject-or-not-to-reject.js - []src/05-always-asynchronous.js - []src/06-shortcuts.js - []src/07-promise-after-promise.js - []src/08-values-and-promises.js []src/09-throw-an-error.js * see note below - []src/10-an-important-file.js * see note below - []src/11-multiple-promises.js - []src/12-fetch-json.js - []src/13-do-some-work.js + [x]Exercises 1-13 in the promise-it-wont-hurt-solutions are complete and written to the appropriate file: + [x]src/01-warmup.js + [x]src/02-fullfill-a-promise.js + [x]src/03-reject-a-promise.js + [x]src/04-to-reject-or-not-to-reject.js + [x]src/05-always-asynchronous.js + [x]src/06-shortcuts.js + [x]src/07-promise-after-promise.js + [x]src/08-values-and-promises.js [x]src/09-throw-an-error.js * see note below + [x]src/10-an-important-file.js * see note below + [x]src/11-multiple-promises.js + [x]src/12-fetch-json.js + [x]src/13-do-some-work.js