diff --git a/curriculum/challenges/_meta/es6/meta.json b/curriculum/challenges/_meta/es6/meta.json index f0c318689e..5a9fbc337f 100644 --- a/curriculum/challenges/_meta/es6/meta.json +++ b/curriculum/challenges/_meta/es6/meta.json @@ -115,8 +115,48 @@ [ "587d7b8d367417b2b2512b59", "Import a Default Export" + ], + [ + "5cdafbb0291309899753167f", +<<<<<<< HEAD +<<<<<<< HEAD + "Create a JavaScript Promise" + ], + [ + "5cdafbc32913098997531680", + "Complete a Promise with Resolve and Reject" + ], + [ + "5cdafbd72913098997531681", + "Handle a Fulfilled Promise with then" + ], + [ + "5cdafbe72913098997531682", + "Handle a Rejected Promise with catch" +======= + "Create a Javascript Promise" +======= + "Create a JavaScript Promise" +>>>>>>> 950f80a92... Update curriculum/challenges/_meta/es6/meta.json + ], + [ + "5cdafbc32913098997531680", + "Complete a Promise with Resolve and Reject" + ], + [ + "5cdafbd72913098997531681", + "Handle a Fulfilled Promise with then" + ], + [ + "5cdafbe72913098997531682", +<<<<<<< HEAD + "Handle a Rejected Promise with .catch" +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= + "Handle a Rejected Promise with catch" +>>>>>>> 8dec9d7c7... fix/add-tests-rename-files ] ], "helpRoom": "Help", "fileName": "02-javascript-algorithms-and-data-structures/es6.json" -} \ No newline at end of file +} diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject.english.md new file mode 100644 index 0000000000..051ef832c4 --- /dev/null +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject.english.md @@ -0,0 +1,221 @@ +--- +id: 5cdafbc32913098997531680 +title: Complete a Promise with Resolve and Reject +challengeType: 1 +--- + +## Description +
+<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +A promise has three states: pending, fulfilled, and rejected. The promise you created in the last challenge is forever stuck in the pending state because you did not add a way to complete the promise. The resolve and reject parameters given to the promise argument are used to do this. resolve is used when you want your promise to succeed, and reject is used when you want it to fail. These are methods that take an argument, as seen below. + +```js +const myPromise = new Promise((resolve, reject) => { + if(true) { + resolve("Promise was fulfilled"); + } else { + reject("Promise was rejected"); + } +======= +A promise has three states: pending, fulfilled, and rejected. The promise you created in the last challenge is forever stuck in the pending state because you did add a way to complete the promise. The two parameters given to the promise function are used to do this. resolve is used when you want your promise to succeed, and reject is used when you want it to fail. These are functions that take an argument as seen below. +======= +A promise has three states: pending, fulfilled, and rejected. The promise you created in the last challenge is forever stuck in the pending state because you did add a way to complete the promise. The resolve and reject parameters given to the promise argument are used to do this. resolve is used when you want your promise to succeed, and reject is used when you want it to fail. These are methods that take an argument, as seen below. +>>>>>>> 396e6142b... fix/update-verbiage +======= +A promise has three states: pending, fulfilled, and rejected. The promise you created in the last challenge is forever stuck in the pending state because you did not add a way to complete the promise. The resolve and reject parameters given to the promise argument are used to do this. resolve is used when you want your promise to succeed, and reject is used when you want it to fail. These are methods that take an argument, as seen below. +>>>>>>> aac94c586... Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject.english.md + +```js +const myPromise = new Promise((resolve, reject) => { +<<<<<<< HEAD + if(true) { + resolve("Promise was fulfilled"); + } else { + reject("Promise was rejected"); + } +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= + if(true) { + resolve("Promise was fulfilled"); + } else { + reject("Promise was rejected"); + } +>>>>>>> d3ba2b0ff... fix/replace-tab-with-spaces +}); +``` + +The example above uses strings for the argument of these functions, but it can really be anything. Often, it might be an object that you would use data from to put on your website or elsewhere. +
+ +## Instructions +
+<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +Make the promise handle success and failure. If responseFromServer is true, call the resolve method to successfully complete the promise. Pass resolve a string with the value We got the data. If responseFromServer is false, use the reject method instead and pass it the string: Data not received. +======= +Use the resolve function if the condition is true to fulfill the promise. Pass it the string: We got the data. as its argument. Use the reject function if condition is not true and pass it the string: Data not received.. +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= +Use the resolve method if the condition given is true to fulfill the promise. Pass it the string: We got the data. as its argument. Use the reject method if condition is not true and pass it the string: Data not received.. +>>>>>>> 396e6142b... fix/update-verbiage +======= +Use the resolve method if the condition given is true to fulfill the promise. Pass it a string with the value We got the data. Use the reject method if condition is false and pass it the string: Data not received. +>>>>>>> 8dec9d7c7... fix/add-tests-rename-files +======= +Make the promise handle success and failure. If responseFromServer is true, call the resolve method to successfully complete the promise. Pass resolve a string with the value We got the data. If the condition is false, use the reject method instead and pass it the string: Data not received. +>>>>>>> 3fa1528b7... Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject.english.md +======= +Make the promise handle success and failure. If responseFromServer is true, call the resolve method to successfully complete the promise. Pass resolve a string with the value We got the data. If responseFromServer is false, use the reject method instead and pass it the string: Data not received. +>>>>>>> 017c4ec24... fix/add-suggested-change-to-instructions +
+ +## Tests +
+ +```yml +tests: +<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD + - text: resolve should be executed when the if condition is true. + testString: assert(removeJSComments(code).match(/if\s*\(\s*responseFromServer\s*\)\s*{\s*resolve\s*\(\s*('|"|`)We got the data\1\s*\)(\s*|\s*;\s*)}/g)); + - text: reject should be executed when the if condition is false. + testString: assert(removeJSComments(code).match(/}\s*else\s*{\s*reject\s*\(\s*('|"|`)Data not received\1\s*\)(\s*|\s*;\s*)}/g)); +======= + - text: var should not exist in code. +======= + - text: You should properly add a resolve method. + testString: getUserInput => assert(!getUserInput('index').match(/var/g),'var should not exist in code.'); + - text: You should properly add a reject method. + testString: getUserInput => assert(!getUserInput('index').match(/var/g),'var should not exist in code.'); + - text: resolve is executed when the if condition is true. + testString: getUserInput => assert(!getUserInput('index').match(/var/g),'var should not exist in code.'); + - text: reject is executed when the if condition is not true. +>>>>>>> 396e6142b... fix/update-verbiage + testString: getUserInput => assert(!getUserInput('index').match(/var/g),'var should not exist in code.'); +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= + - text: resolve should be executed when the if condition is true. + testString: assert(removeJSComments(code).match(/if\s*\(\s*responseFromServer\s*\)\s*{\s*resolve\s*\(\s*('|"|`)We got the data\1\s*\)(\s*|\s*;\s*)}/g)); + - text: reject should be executed when the if condition is false. + testString: assert(removeJSComments(code).match(/}\s*else\s*{\s*reject\s*\(\s*('|"|`)Data not received\1\s*\)(\s*|\s*;\s*)}/g)); +>>>>>>> 8dec9d7c7... fix/add-tests-rename-files +``` + +
+ +## Challenge Seed +
+
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { +<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +// responseFromServer represents a response from a server +let responseFromServer; + + if(responseFromServer) { + // change this line + } else { + // change this line + } +======= + const success = true; +======= + const responseFromServer = true; +>>>>>>> 396e6142b... fix/update-verbiage + + if(responseFromServer) { + // change this line + } else { + // change this line + } +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= + const responseFromServer = true; +======= +// responseFromServer represents a response from a server +let responseFromServer; +>>>>>>> 466610bc8... Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject.english.md + + if(responseFromServer) { + // change this line + } else { + // change this line + } +>>>>>>> d3ba2b0ff... fix/replace-tab-with-spaces +}); +``` + +
+<<<<<<< HEAD +<<<<<<< HEAD +======= +>>>>>>> 8dec9d7c7... fix/add-tests-rename-files + +### After Test +
+ +```js +const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); +``` + +
+<<<<<<< HEAD +======= +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= +>>>>>>> 8dec9d7c7... fix/add-tests-rename-files +
+ +## Solution +
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { +<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +// responseFromServer represents a response from a server +let responseFromServer; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); +======= + const success = true; +======= + const responseFromServer = true; +>>>>>>> 396e6142b... fix/update-verbiage +======= + const responseFromServer = true; +>>>>>>> d3ba2b0ff... fix/replace-tab-with-spaces +======= +// responseFromServer represents a response from a server +let responseFromServer; +>>>>>>> 5a10880b6... fix/add-suggested-changes-to-seeds-and-solutions + + if(responseFromServer) { + resolve("We got the data"); + } else { +<<<<<<< HEAD + reject("Data not received."); +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= + reject("Data not received"); +>>>>>>> 8dec9d7c7... fix/add-tests-rename-files + } +}); +``` + +
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.english.md new file mode 100644 index 0000000000..400257a9dd --- /dev/null +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.english.md @@ -0,0 +1,122 @@ +--- +id: 5cdafbb0291309899753167f +<<<<<<< HEAD +<<<<<<< HEAD +title: Create a JavaScript Promise +======= +title: Create a Javascript Promise +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= +title: Create a JavaScript Promise +>>>>>>> bed9cbc73... Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.english.md +challengeType: 1 +--- + +## Description +
+<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +<<<<<<< HEAD +A promise in JavaScript is exactly what it sounds like. You use it to make a promise to do something, usually asynchronously. When the task completes you either fulfill your promise or fail to do so. Promise is a constructor function so you need to use the new keyword to create one. It takes a function as its argument with two parameters, resolve and reject. These are methods used to determine the outcome of the promise. The syntax looks like this: +======= +A promise in javascript is exactly what it sounds like. You use it to make a promise to do something, and at some point you either fulfill your promise or fail to do so. It’s a constructor function, so they are create with the new keyword. It needs a function as its argument with two parameters, resolve and reject. These are methods used to determine the outcome of the promise. The syntax looks like this: +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= +A promise in javascript is exactly what it sounds like. You use it to make a promise to do something, usually asynchronous. When the task completes you either fulfill your promise or fail to do so. It’s a constructor function, so they are created with the new keyword. It needs a function as its argument with two parameters, resolve and reject. These are methods used to determine the outcome of the promise. The syntax looks like this: +>>>>>>> 396e6142b... fix/update-verbiage +======= +A promise in JavaScript is exactly what it sounds like. You use it to make a promise to do something, usually asynchronously. When the task completes you either fulfill your promise or fail to do so. Promise is a constructor function so you need to use the new keyword to create one. It needs a function as its argument with two parameters, resolve and reject. These are methods used to determine the outcome of the promise. The syntax looks like this: +>>>>>>> d86e97264... Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.english.md +======= +A promise in JavaScript is exactly what it sounds like. You use it to make a promise to do something, usually asynchronously. When the task completes you either fulfill your promise or fail to do so. Promise is a constructor function so you need to use the new keyword to create one. It takes a function as its argument with two parameters, resolve and reject. These are methods used to determine the outcome of the promise. The syntax looks like this: +>>>>>>> 915a966f1... fix/change-needs-to-takes-in-instructions + +```js +const myPromise = new Promise((resolve, reject) => { + +}); +``` + +
+ +## Instructions +
+<<<<<<< HEAD +<<<<<<< HEAD +Create a new promise called makeServerRequest. Pass in a function with resolve and reject parameters to the constructor. +======= +Create a new promise called makeServerRequest. Pass in a function with resolve and reject parameters to the promise. +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= +Create a new promise called makeServerRequest. Pass in a function with resolve and reject parameters to the constructor. +>>>>>>> 79a812601... Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.english.md +
+ +## Tests +
+ +```yml +tests: +<<<<<<< HEAD +<<<<<<< HEAD + - text: You should assign a promise to a declared variable named makeServerRequest. + testString: assert(makeServerRequest instanceof Promise); + - text: Your promise should receive a function with resolve and reject as parameters. + testString: assert(code.match(/Promise\(\s*(function\s*\(\s*resolve\s*,\s*reject\s*\)\s*{|\(\s*resolve\s*,\s*reject\s*\)\s*=>\s*{)[^}]*}/g)); +======= + - text: Your promise should be in a variable called makeServerRequest. +======= + - text: You should assign a promise to a declared variable named makeServerRequest. +>>>>>>> 8f4cfb0e5... Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.english.md + testString: assert(makeServerRequest instanceof Promise); + - text: Your promise should receive a function with resolve and reject as parameters. +<<<<<<< HEAD +<<<<<<< HEAD + testString: assert(typeof(makeServerRequest) === "object" && (code.match(/new\s*Promise\(\s*\(\s*resolve\s*,\s*reject\s*\)\s*=>\s*{/g) || code.match(/new\s*Promise\s*\(\s*function\s*\(\s*resolve\s*,\s*reject\s*\)\s*{/g))); +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= + testString: assert(makeServerRequest instanceof Promise && (code.match(/new\s*Promise\(\s*\(\s*resolve\s*,\s*reject\s*\)\s*=>\s*{/g) || code.match(/new\s*Promise\s*\(\s*function\s*\(\s*resolve\s*,\s*reject\s*\)\s*{/g))); +>>>>>>> 066e1792e... fix/add-tests +======= + testString: assert(code.match(/Promise\(\s*(function\s*\(\s*resolve\s*,\s*reject\s*\)\s*{|\(\s*resolve\s*,\s*reject\s*\)\s*=>\s*{)[^}]*}/g)); +>>>>>>> 2a76bf50c... fix/make-tests-more-robust +``` + +
+ +## Challenge Seed +
+
+ +```js +<<<<<<< HEAD +<<<<<<< HEAD + +======= +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= + +>>>>>>> 2a76bf50c... fix/make-tests-more-robust +``` + +
+
+ +## Solution +
+ +```js +<<<<<<< HEAD +<<<<<<< HEAD +const makeServerRequest = new Promise((resolve, reject) => { +======= +makeServerRequest = new Promise((resolve, reject) => { +>>>>>>> 5aba19817... feat/new-lessons-on-js-promises +======= +const makeServerRequest = new Promise((resolve, reject) => { +>>>>>>> e40baddf2... fix/solution-to-pass-tests + +}); +``` +
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then.english.md new file mode 100644 index 0000000000..623ced0644 --- /dev/null +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then.english.md @@ -0,0 +1,90 @@ +--- +id: 5cdafbd72913098997531681 +title: Handle a Fulfilled Promise with then +challengeType: 1 +--- + +## Description +
+Promises are most useful when you have a process that takes an unknown amount of time in your code (i.e. something asynchronous). Often, a server request. When you make a server request it takes some amount of time, and after it completes you usually want to do something with the response from the server. This can be achieved by using the then method. The then method is executed immediately after your promise is fulfilled with resolve. Here’s an example: + +```js +myPromise.then(result => { + // do something with the result. +}); +``` + +result comes from the argument given to the resolve method. +
+ +## Instructions +
+Add the then method to your promise. Use result as the argument of its callback function and log result to the console. +
+ +## Tests +
+ +```yml +tests: + - text: You should call the then method on the promise. + testString: assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.then\(/g)); + - text: Your then method should have a callback function with result as its parameter. + testString: assert(resultIsParameter); + - text: You should log result to the console. + testString: assert(resultIsParameter && codeWithoutSpaces.match(/\.then\(.*?result.*?console.log\(result\).*?\)/)); +``` + +
+ +## Challenge Seed +
+
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to true to represent a successful response from a server + let responseFromServer = true; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); +``` + +
+ +### After Test +
+ +```js +const codeWithoutSpaces = code.replace(/\s/g, ''); +const resultIsParameter = /\.then\((function\(result\){|result|\(result\)=>)/.test(codeWithoutSpaces); +``` + +
+
+ +## Solution +
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to true to represent a successful response from a server + let responseFromServer = true; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); +``` + +
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then.english.md~HEAD b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then.english.md~HEAD new file mode 100644 index 0000000000..623ced0644 --- /dev/null +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then.english.md~HEAD @@ -0,0 +1,90 @@ +--- +id: 5cdafbd72913098997531681 +title: Handle a Fulfilled Promise with then +challengeType: 1 +--- + +## Description +
+Promises are most useful when you have a process that takes an unknown amount of time in your code (i.e. something asynchronous). Often, a server request. When you make a server request it takes some amount of time, and after it completes you usually want to do something with the response from the server. This can be achieved by using the then method. The then method is executed immediately after your promise is fulfilled with resolve. Here’s an example: + +```js +myPromise.then(result => { + // do something with the result. +}); +``` + +result comes from the argument given to the resolve method. +
+ +## Instructions +
+Add the then method to your promise. Use result as the argument of its callback function and log result to the console. +
+ +## Tests +
+ +```yml +tests: + - text: You should call the then method on the promise. + testString: assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.then\(/g)); + - text: Your then method should have a callback function with result as its parameter. + testString: assert(resultIsParameter); + - text: You should log result to the console. + testString: assert(resultIsParameter && codeWithoutSpaces.match(/\.then\(.*?result.*?console.log\(result\).*?\)/)); +``` + +
+ +## Challenge Seed +
+
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to true to represent a successful response from a server + let responseFromServer = true; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); +``` + +
+ +### After Test +
+ +```js +const codeWithoutSpaces = code.replace(/\s/g, ''); +const resultIsParameter = /\.then\((function\(result\){|result|\(result\)=>)/.test(codeWithoutSpaces); +``` + +
+
+ +## Solution +
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to true to represent a successful response from a server + let responseFromServer = true; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); +``` + +
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch.english.md new file mode 100644 index 0000000000..aa6cb524f9 --- /dev/null +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch.english.md @@ -0,0 +1,101 @@ +--- +id: 5cdafbe72913098997531682 +title: Handle a Rejected Promise with catch +challengeType: 1 +--- + +## Description +
+catch is the method used when your promise has been rejected. It is executed immediately after a promise's reject method is called. Here’s the syntax: + +```js +myPromise.catch(error => { + // do something with the error. +}); +``` + +error is the argument passed in to the reject method. + +Note: the then and catch methods can be chained to the promise declaration if you chose. +
+ +## Instructions +
+Add the catch method to your promise. Use error as the argument of its callback function and log error to the console. +
+ +## Tests +
+ +```yml +tests: + - text: You should call the catch method on the promise. + testString: assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.catch\(/g)); + - text: Your catch method should have a callback function with error as its parameter. + testString: assert(errorIsParameter); + - text: You should log error to the console. + testString: assert(errorIsParameter && codeWithoutSpaces.match(/\.catch\(.*?error.*?console.log\(error\).*?\)/)); +``` + +
+ +## Challenge Seed +
+
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to false to represent an unsuccessful response from a server + let responseFromServer = false; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); +``` + +
+ +### After Test +
+ +```js +const codeWithoutSpaces = code.replace(/\s/g, ''); +const errorIsParameter = /\.catch\((function\(error\){|error|\(error\)=>)/.test(codeWithoutSpaces); +``` + +
+ +
+ +## Solution +
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to false to represent an unsuccessful response from a server + let responseFromServer = false; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); + +makeServerRequest.catch(error => { + console.log(error); +}); +``` + +
diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch.english.md~HEAD b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch.english.md~HEAD new file mode 100644 index 0000000000..aa6cb524f9 --- /dev/null +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch.english.md~HEAD @@ -0,0 +1,101 @@ +--- +id: 5cdafbe72913098997531682 +title: Handle a Rejected Promise with catch +challengeType: 1 +--- + +## Description +
+catch is the method used when your promise has been rejected. It is executed immediately after a promise's reject method is called. Here’s the syntax: + +```js +myPromise.catch(error => { + // do something with the error. +}); +``` + +error is the argument passed in to the reject method. + +Note: the then and catch methods can be chained to the promise declaration if you chose. +
+ +## Instructions +
+Add the catch method to your promise. Use error as the argument of its callback function and log error to the console. +
+ +## Tests +
+ +```yml +tests: + - text: You should call the catch method on the promise. + testString: assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.catch\(/g)); + - text: Your catch method should have a callback function with error as its parameter. + testString: assert(errorIsParameter); + - text: You should log error to the console. + testString: assert(errorIsParameter && codeWithoutSpaces.match(/\.catch\(.*?error.*?console.log\(error\).*?\)/)); +``` + +
+ +## Challenge Seed +
+
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to false to represent an unsuccessful response from a server + let responseFromServer = false; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); +``` + +
+ +### After Test +
+ +```js +const codeWithoutSpaces = code.replace(/\s/g, ''); +const errorIsParameter = /\.catch\((function\(error\){|error|\(error\)=>)/.test(codeWithoutSpaces); +``` + +
+ +
+ +## Solution +
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to false to represent an unsuccessful response from a server + let responseFromServer = false; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); + +makeServerRequest.catch(error => { + console.log(error); +}); +``` + +