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.
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
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
```