2.6 KiB
2.6 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
5cdafbd72913098997531681 | Handle a Fulfilled Promise with then | 1 | 301203 |
Description
then
method. The then
method is executed immediately after your promise is fulfilled with resolve
. Here’s an example:
myPromise.then(result => {
// do something with the result.
});
result
comes from the argument given to the resolve
method.
Instructions
then
method to your promise. Use result
as the parameter of its callback function and log result
to the console.
Tests
tests:
- text: You should call the <code>then</code> method on the promise.
testString: assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.then\(/g));
- text: Your <code>then</code> method should have a callback function with <code>result</code> as its parameter.
testString: assert(resultIsParameter);
- text: You should log <code>result</code> to the console.
testString: assert(resultIsParameter && codeWithoutSpaces.match(/\.then\(.*?result.*?console.log\(result\).*?\)/));
Challenge Seed
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
const codeWithoutSpaces = code.replace(/\s/g, '');
const resultIsParameter = /\.then\((function\(result\){|result|\(result\)=>)/.test(codeWithoutSpaces);
Solution
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);
});