freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../es6/complete-a-promise-with-res...

86 lines
2.4 KiB
Markdown
Raw Normal View History

2019-05-14 19:52:01 +00:00
---
id: 5cdafbc32913098997531680
2019-07-03 12:18:59 +00:00
title: Complete a Promise with resolve and reject
2019-05-14 19:52:01 +00:00
challengeType: 1
forumTopicId: 301196
dashedName: complete-a-promise-with-resolve-and-reject
2019-05-14 19:52:01 +00:00
---
# --description--
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.
2019-05-14 19:52:01 +00:00
```js
const myPromise = new Promise((resolve, reject) => {
if(condition here) {
2019-05-14 19:52:01 +00:00
resolve("Promise was fulfilled");
} else {
reject("Promise was rejected");
}
});
2019-05-14 19:52:01 +00:00
```
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.
2019-05-14 19:52:01 +00:00
# --instructions--
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`.
# --hints--
2019-05-14 19:52:01 +00:00
`resolve` should be called with the expected string when the `if` condition is `true`.
```js
assert(
__helpers
.removeJSComments(code)
.match(
/if\s*\(\s*responseFromServer\s*\)\s*{\s*resolve\s*\(\s*('|"|`)We got the data\1\s*\)(\s*|\s*;\s*)}/g
)
);
```
2019-05-14 19:52:01 +00:00
`reject` should be called with the expected string when the `if` condition is `false`.
```js
assert(
__helpers
.removeJSComments(code)
.match(
/}\s*else\s*{\s*reject\s*\(\s*('|"|`)Data not received\1\s*\)(\s*|\s*;\s*)}/g
)
);
2019-05-14 19:52:01 +00:00
```
# --seed--
2019-05-14 19:52:01 +00:00
## --seed-contents--
2019-05-14 19:52:01 +00:00
```js
const makeServerRequest = new Promise((resolve, reject) => {
2019-07-02 14:29:41 +00:00
// responseFromServer represents a response from a server
let responseFromServer;
2019-05-14 19:52:01 +00:00
if(responseFromServer) {
// Change this line
} else {
// Change this line
2019-05-14 19:52:01 +00:00
}
});
```
# --solutions--
2019-05-14 19:52:01 +00:00
```js
const makeServerRequest = new Promise((resolve, reject) => {
2019-07-02 14:29:41 +00:00
// responseFromServer represents a response from a server
let responseFromServer;
2019-05-14 19:52:01 +00:00
if(responseFromServer) {
resolve("We got the data");
} else {
2019-05-14 19:52:01 +00:00
reject("Data not received");
}
});
```