Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new Promise that resolves with 'Done'.
Node.js
const myPromise = new Promise((resolve, reject) => {
resolve([1]);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using reject instead of resolve
✗ Incorrect
The Promise resolves with the string 'Done', so it must be in quotes.
2fill in blank
mediumComplete the code to handle the Promise result with then().
Node.js
myPromise.[1](result => {
console.log(result);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using catch instead of then for success
Trying to use resolve as a method
✗ Incorrect
The then() method is used to handle the resolved value of a Promise.
3fill in blank
hardFix the error in the Promise chain to catch errors.
Node.js
myPromise.then(result => {
console.log(result);
}).[1](error => {
console.error(error);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using then instead of catch for errors
Using finally to catch errors
✗ Incorrect
The catch() method handles errors in the Promise chain.
4fill in blank
hardFill both blanks to create a Promise that rejects on error and handle it.
Node.js
const errorPromise = new Promise((resolve, reject) => {
if (somethingWrong) {
reject([1]);
} else {
resolve('Success');
}
});
errorPromise.[2](error => {
console.log(error);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using resolve instead of reject
Using then instead of catch for errors
✗ Incorrect
The Promise rejects with an error message string, and catch() handles the rejection.
5fill in blank
hardFill all three blanks to create a Promise, handle success and error, and finally log done.
Node.js
const promise = new Promise((resolve, reject) => {
if (isReady) {
resolve([1]);
} else {
reject([2]);
}
});
promise
.then(result => console.log(result))
.catch(error => console.error(error))
.[3](() => console.log('Done')); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using catch instead of finally for the last step
Forgetting quotes around messages
✗ Incorrect
The Promise resolves with 'All good', rejects with 'Failed', and finally() runs after either case.