Skip to main content

Promise Waterfall

MIDDLE

Promise Waterfall

Execute async functions in sequence, passing the result of each to the next.

Requirements:

  • Implement promiseWaterfall that takes array of async functions
  • Execute functions sequentially
  • Pass the result of each function as input to the next
  • Return the final result

Example:

const add5 = async (n) => n + 5;
const multiply2 = async (n) => n * 2;
const subtract3 = async (n) => n - 3;

promiseWaterfall([add5, multiply2, subtract3], 10).then(result => {
  console.log(result); // ((10 + 5) * 2) - 3 = 27
});

Examples:

Input 1:{"tasks":[null,null],"initialValue":10}
Output 1:30
Input 2:{"tasks":[null,null],"initialValue":0}
Output 2:2

Loading editor...

Run your code to see results

Click the Run button above