Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new Promise.
Javascript
const myPromise = new [1]((resolve, reject) => { resolve('Done'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'promise' instead of 'Promise'.
Using 'Function' instead of 'Promise'.
✗ Incorrect
Promises in JavaScript are created using the Promise constructor.
2fill in blank
mediumComplete the code to handle a resolved Promise.
Javascript
myPromise.[1](result => {
console.log(result);
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'catch' to handle success.
Using 'resolve' as a method.
✗ Incorrect
The then method is used to handle the result when a Promise is resolved.
3fill in blank
hardFix the error in the code to handle Promise rejection.
Javascript
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' to catch errors.
Using 'finally' to catch errors.
✗ Incorrect
The catch method is used to handle errors when a Promise is rejected.
4fill in blank
hardFill both blanks to create a Promise that rejects on error.
Javascript
const errorPromise = new Promise(([1], [2]) => { [1]('Success'); [2]('Error occurred'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping resolve and reject.
Using variable names instead of function names.
✗ Incorrect
The first parameter is resolve to handle success, the second is reject to handle errors.
5fill in blank
hardFill all three blanks to create a Promise and handle both success and failure.
Javascript
const promise = new Promise(([1], [2]) => { if(true) { [1]('Done'); } else { [2]('Failed'); } }); promise.[3](message => console.log(message)).catch(error => console.error(error));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'finally' instead of 'then'.
Mixing up resolve and reject.
✗ Incorrect
Use resolve and reject in the Promise constructor, and then to handle success.