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---------------- */ diff --git a/promise-it-wont-hurt-solutions/README.md b/promise-it-wont-hurt-solutions/README.md index e03f7a0..a2f823f 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: + [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 + + [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 + + [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 diff --git a/promise-it-wont-hurt-solutions/src/01-warmup.js b/promise-it-wont-hurt-solutions/src/01-warmup.js index e69de29..8a24f62 100644 --- a/promise-it-wont-hurt-solutions/src/01-warmup.js +++ b/promise-it-wont-hurt-solutions/src/01-warmup.js @@ -0,0 +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!'); +} + +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..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 @@ -0,0 +1,28 @@ + var promise = new Promise(function (fulfill, reject) { + // Your solution here + setTimeout(function(){ + fulfill('FULFILLED!'); + }) + + }, 3000); + promise.then((successMessage) => { + console.log(); + }); + + + + + + + // 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 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..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 @@ -0,0 +1,17 @@ + + + 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 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 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 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 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..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 @@ -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(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 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 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 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..bcb6914 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,65 @@ +// function all(promises) { +// console.log(promises) +// let counter = 0 +// // let promises = [] + +// function count() { +// // counter++ +// if (counter === 2) resolve(promises) +// } + +// return new Promise((resolve, reject) => { + + +// 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/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)); +}); + + 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..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 @@ -0,0 +1,35 @@ +// 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); + +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