Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new Promise that resolves with 'Done'.
Javascript
const promise = new Promise(([1]) => { resolve('Done'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'reject' instead of 'resolve' when fulfilling the promise.
Using a parameter name that is not 'resolve' or 'reject'.
✗ Incorrect
The Promise constructor takes a function with 'resolve' and 'reject' parameters. Here, we use 'resolve' to fulfill the promise.
2fill in blank
mediumComplete the code to create a Promise that rejects with an error message.
Javascript
const promise = new Promise((resolve, [1]) => { [1]('Error occurred'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'resolve' instead of 'reject' to reject the promise.
Using incorrect parameter names.
✗ Incorrect
The second parameter of the Promise executor function is 'reject', used to reject the promise with an error.
3fill in blank
hardFix the error in the Promise executor function parameter to correctly create a Promise.
Javascript
const promise = new Promise([1] => { resolve('Success'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single parameter without parentheses.
Omitting the reject parameter.
✗ Incorrect
The Promise executor function must have two parameters: resolve and reject, enclosed in parentheses.
4fill in blank
hardFill both blanks to create a Promise that resolves after 1 second with 'Done'.
Javascript
const promise = new Promise(([1], [2]) => { setTimeout(() => [1]('Done'), 1000); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names.
Calling reject instead of resolve inside setTimeout.
✗ Incorrect
The Promise executor function takes 'resolve' and 'reject'. We call 'resolve' inside setTimeout to fulfill the promise after 1 second.
5fill in blank
hardFill all three blanks to create a Promise that resolves with the uppercase version of input after 500ms.
Javascript
function toUpperAsync(str) {
return new Promise(([1], [2]) => {
setTimeout(() => [1](str[3]), 500);
});
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .toLowerCase() instead of .toUpperCase().
Calling reject instead of resolve.
Missing parentheses around parameters.
✗ Incorrect
The Promise executor uses 'resolve' and 'reject'. We call resolve with str.toUpperCase() after 500ms.