10 KiB
10 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
5cdafbc32913098997531680 | Complete a Promise with Resolve and Reject | 1 |
Description
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.
const myPromise = new Promise((resolve, reject) => {
if(true) {
resolve("Promise was fulfilled");
} else {
reject("Promise was rejected");
}
=======
A promise has three states: <code>pending</code>, <code>fulfilled</code>, and <code>rejected</code>. The promise you created in the last challenge is forever stuck in the <code>pending</code> state because you did add a way to complete the promise. The two parameters given to the promise function are used to do this. <code>resolve</code> is used when you want your promise to succeed, and <code>reject</code> is used when you want it to fail. These are functions that take an argument as seen below.
=======
A promise has three states: <code>pending</code>, <code>fulfilled</code>, and <code>rejected</code>. The promise you created in the last challenge is forever stuck in the <code>pending</code> state because you did add a way to complete the promise. The <code>resolve</code> and <code>reject</code> parameters given to the promise argument are used to do this. <code>resolve</code> is used when you want your promise to succeed, and <code>reject</code> 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: <code>pending</code>, <code>fulfilled</code>, and <code>rejected</code>. The promise you created in the last challenge is forever stuck in the <code>pending</code> state because you did not add a way to complete the promise. The <code>resolve</code> and <code>reject</code> parameters given to the promise argument are used to do this. <code>resolve</code> is used when you want your promise to succeed, and <code>reject</code> 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
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
tests:
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
- text: <code>resolve</code> should be executed when the <code>if</code> condition is <code>true</code>.
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: <code>reject</code> should be executed when the <code>if</code> condition is <code>false</code>.
testString: assert(removeJSComments(code).match(/}\s*else\s*{\s*reject\s*\(\s*('|"|`)Data not received\1\s*\)(\s*|\s*;\s*)}/g));
=======
- text: <code>var</code> should not exist in code.
=======
- text: You should properly add a <code>resolve</code> method.
testString: getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> should not exist in code.');
- text: You should properly add a <code>reject</code> method.
testString: getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> should not exist in code.');
- text: <code>resolve</code> is executed when the <code>if</code> condition is true.
testString: getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> should not exist in code.');
- text: <code>reject</code> is executed when the <code>if</code> condition is not true.
>>>>>>> 396e6142b... fix/update-verbiage
testString: getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> should not exist in code.');
>>>>>>> 5aba19817... feat/new-lessons-on-js-promises
=======
- text: <code>resolve</code> should be executed when the <code>if</code> condition is <code>true</code>.
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: <code>reject</code> should be executed when the <code>if</code> condition is <code>false</code>.
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
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
});
After Test
const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');
Solution
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
}
});