Promises - Exercise 3

Question 1: Collect Resolved Values

Use Promise.all to resolve the supplied array of values and return the values in their original order.

Expected output: [3,1,2] (print this with console.log)

javascript

function collectPromiseValues(values) {
  // Write your solution here
}

console.log(await collectPromiseValues([3,1,2]));

Question 2: Sum Resolved Values

Use Promise.all to resolve each number, then return their sum. An empty input sums to zero.

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

javascript

function sumPromiseValues(values) {
  // Write your solution here
}

console.log(await sumPromiseValues([2,3,4]));

Question 3: Pair Two Promise Results

Use Promise.all to resolve both supplied values and return the pair in argument order.

Expected output: ["a","b"] (print this with console.log)

javascript

function pairPromiseResults(first, second) {
  // Write your solution here
}

console.log(await pairPromiseResults("a", "b"));

Question 4: Recover a Rejected Promise All

Use Promise.all with one resolved value and one rejected reason. Catch the rejection and return that reason.

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

javascript

function recoverPromiseAllRejection(value, reason) {
  // Write your solution here
}

console.log(await recoverPromiseAllRejection("ok", "failed"));

Question 5: Read Settled Statuses

For each boolean, create a resolved promise when true or a rejected promise when false. Use Promise.allSettled and return the status strings in order.

Expected output: ["fulfilled","rejected"] (print this with console.log)

javascript

function settledPromiseStatuses(flags) {
  // Write your solution here
}

console.log(await settledPromiseStatuses([true,false]));

Question 6: Read Settled Result Values

Use Promise.allSettled with a resolved value and rejected reason. Return an array with the fulfilled value and rejection reason.

Expected output: ["ok","bad"] (print this with console.log)

javascript

function settledPromiseDetails(value, reason) {
  // Write your solution here
}

console.log(await settledPromiseDetails("ok", "bad"));

Question 7: Select the First Settled Promise

Use Promise.race with two already resolved values and return the winning value. The first input is inserted first.

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

javascript

function raceResolvedValues(first, second) {
  // Write your solution here
}

console.log(await raceResolvedValues("first", "second"));

Question 8: Find the First Fulfilled Promise

Use Promise.any with a rejected reason followed by a resolved value, and return the fulfilled value.

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

javascript

function anyPromiseFulfilledValue(reason, value) {
  // Write your solution here
}

console.log(await anyPromiseFulfilledValue("bad", "good"));

Question 9: Collect Rejection Reasons

Use Promise.allSettled on the supplied strings as rejection reasons and return the reasons in order.

Expected output: ["a","b"] (print this with console.log)

javascript

function collectSettledRejectionReasons(reasons) {
  // Write your solution here
}

console.log(await collectSettledRejectionReasons(["a","b"]));

Question 10: Double All Resolved Numbers

Resolve all supplied numbers with Promise.all, then return a new array with each number doubled.

Expected output: [4,8] (print this with console.log)

javascript

function doubleAllPromiseNumbers(numbers) {
  // Write your solution here
}

console.log(await doubleAllPromiseNumbers([2,4]));