Promises - Exercise 2
Question 1: Recover from a Rejection Completed
Reject with the supplied message, catch it, and resolve with that message.
Expected output: "unavailable" (print this with console.log)
function recoverRejectedMessage(message) {
// Write your solution here
}
console.log(await recoverRejectedMessage("unavailable"));function recoverRejectedMessage(message) {
return Promise.reject(message).catch((reason) => reason);
}
console.log(await recoverRejectedMessage("unavailable"));Question 2: Provide a Rejection Fallback Completed
Reject with the supplied reason and use catch to return the supplied fallback.
Expected output: "default" (print this with console.log)
function promiseRejectionFallback(reason, fallback) {
// Write your solution here
}
console.log(await promiseRejectionFallback("missing", "default"));function promiseRejectionFallback(reason, fallback) {
return Promise.reject(reason).catch(() => fallback);
}
console.log(await promiseRejectionFallback("missing", "default"));Question 3: Read a Rejected Error Completed
Reject with an Error using the supplied message. Catch it and return its message.
Expected output: "failed" (print this with console.log)
function readRejectedErrorMessage(message) {
// Write your solution here
}
console.log(await readRejectedErrorMessage("failed"));function readRejectedErrorMessage(message) {
return Promise.reject(new Error(message)).catch((error) => error.message);
}
console.log(await readRejectedErrorMessage("failed"));Question 4: Recover a Failed Calculation Completed
Resolve the doubled number for non-negative input. For negative input reject and catch the error, returning zero.
Expected output: 0 (print this with console.log)
function recoverNegativePromiseInput(number) {
// Write your solution here
}
console.log(await recoverNegativePromiseInput(-2));function recoverNegativePromiseInput(number) {
return (number < 0 ? Promise.reject(new Error("negative")) : Promise.resolve(number * 2)).catch(() => 0);
}
console.log(await recoverNegativePromiseInput(-2));Question 5: Catch a Thrown Then Error Completed
Resolve the input, throw an Error with that input as its message in then, and catch it to return the message.
Expected output: "problem" (print this with console.log)
function catchThrownThenError(message) {
// Write your solution here
}
console.log(await catchThrownThenError("problem"));function catchThrownThenError(message) {
return Promise.resolve(message).then((value) => { throw new Error(value); }).catch((error) => error.message);
}
console.log(await catchThrownThenError("problem"));Question 6: Continue After a Catch Completed
Reject with the supplied text, recover with it in catch, then return it prefixed with "Recovered: ".
Expected output: "Recovered: offline" (print this with console.log)
function continueAfterPromiseCatch(text) {
// Write your solution here
}
console.log(await continueAfterPromiseCatch("offline"));function continueAfterPromiseCatch(text) {
return Promise.reject(text).catch((reason) => reason).then((value) => "Recovered: " + value);
}
console.log(await continueAfterPromiseCatch("offline"));Question 7: Use Finally After Success Completed
Resolve the supplied value. Use finally to set a local boolean to true, then return an array with the value and that boolean.
Expected output: [8,true] (print this with console.log)
function promiseFinallyAfterSuccess(value) {
// Write your solution here
}
console.log(await promiseFinallyAfterSuccess(8));function promiseFinallyAfterSuccess(value) {
let cleaned = false;
return Promise.resolve(value).finally(() => { cleaned = true; }).then((result) => [result, cleaned]);
}
console.log(await promiseFinallyAfterSuccess(8));Question 8: Use Finally After Failure Completed
Reject with the supplied reason. Use finally to set a local boolean to true, then catch and return an array containing the reason and boolean.
Expected output: ["error",true] (print this with console.log)
function promiseFinallyAfterFailure(reason) {
// Write your solution here
}
console.log(await promiseFinallyAfterFailure("error"));function promiseFinallyAfterFailure(reason) {
let cleaned = false;
return Promise.reject(reason).finally(() => { cleaned = true; }).catch((error) => [error, cleaned]);
}
console.log(await promiseFinallyAfterFailure("error"));Question 9: Validate a Resolved Password Completed
Resolve the supplied text. In then, throw an Error with message "short" when its length is below the minimum; catch and return the message. Otherwise return "ok".
Expected output: "short" (print this with console.log)
function validateResolvedPassword(text, minimum) {
// Write your solution here
}
console.log(await validateResolvedPassword("abc", 4));function validateResolvedPassword(text, minimum) {
return Promise.resolve(text).then((value) => { if (value.length < minimum) throw new Error("short"); return "ok"; }).catch((error) => error.message);
}
console.log(await validateResolvedPassword("abc", 4));Question 10: Recover a Numeric Rejection Completed
Reject with the supplied number. Use catch to return its absolute value.
Expected output: 5 (print this with console.log)
function recoverAbsoluteRejection(number) {
// Write your solution here
}
console.log(await recoverAbsoluteRejection(-5));function recoverAbsoluteRejection(number) {
return Promise.reject(number).catch((value) => Math.abs(value));
}
console.log(await recoverAbsoluteRejection(-5));