Complete the code to create a new Promise that resolves with 'Done'.
const myPromise = new Promise((resolve, reject) => {
resolve([1]);
});The Promise resolves with the string 'Done', so it must be in quotes.
Complete the code to handle the Promise result with then().
myPromise.[1](result => {
console.log(result);
});The then() method is used to handle the resolved value of a Promise.
Fix the error in the Promise chain to catch errors.
myPromise.then(result => {
console.log(result);
}).[1](error => {
console.error(error);
});The catch() method handles errors in the Promise chain.
Fill both blanks to create a Promise that rejects on error and handle it.
const errorPromise = new Promise((resolve, reject) => {
if (somethingWrong) {
reject([1]);
} else {
resolve('Success');
}
});
errorPromise.[2](error => {
console.log(error);
});The Promise rejects with an error message string, and catch() handles the rejection.
Fill all three blanks to create a Promise, handle success and error, and finally log done.
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'));The Promise resolves with 'All good', rejects with 'Failed', and finally() runs after either case.
