Promises - Exercise 2

Question 1: Recover from a Rejection

Reject with the supplied message, catch it, and resolve with that message.

Expected output: "unavailable" (print this with console.log)

javascript

function recoverRejectedMessage(message) {
  // Write your solution here
}

console.log(await recoverRejectedMessage("unavailable"));

Question 2: Provide a Rejection Fallback

Reject with the supplied reason and use catch to return the supplied fallback.

Expected output: "default" (print this with console.log)

javascript

function promiseRejectionFallback(reason, fallback) {
  // Write your solution here
}

console.log(await promiseRejectionFallback("missing", "default"));

Question 3: Read a Rejected Error

Reject with an Error using the supplied message. Catch it and return its message.

Expected output: "failed" (print this with console.log)

javascript

function readRejectedErrorMessage(message) {
  // Write your solution here
}

console.log(await readRejectedErrorMessage("failed"));

Question 4: Recover a Failed Calculation

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)

javascript

function recoverNegativePromiseInput(number) {
  // Write your solution here
}

console.log(await recoverNegativePromiseInput(-2));

Question 5: Catch a Thrown Then Error

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)

javascript

function catchThrownThenError(message) {
  // Write your solution here
}

console.log(await catchThrownThenError("problem"));

Question 6: Continue After a Catch

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)

javascript

function continueAfterPromiseCatch(text) {
  // Write your solution here
}

console.log(await continueAfterPromiseCatch("offline"));

Question 7: Use Finally After Success

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)

javascript

function promiseFinallyAfterSuccess(value) {
  // Write your solution here
}

console.log(await promiseFinallyAfterSuccess(8));

Question 8: Use Finally After Failure

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)

javascript

function promiseFinallyAfterFailure(reason) {
  // Write your solution here
}

console.log(await promiseFinallyAfterFailure("error"));

Question 9: Validate a Resolved Password

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)

javascript

function validateResolvedPassword(text, minimum) {
  // Write your solution here
}

console.log(await validateResolvedPassword("abc", 4));

Question 10: Recover a Numeric Rejection

Reject with the supplied number. Use catch to return its absolute value.

Expected output: 5 (print this with console.log)

javascript

function recoverAbsoluteRejection(number) {
  // Write your solution here
}

console.log(await recoverAbsoluteRejection(-5));