Explorer
Node.js

Implement a `createPipeline()` that has `use(fn)` and `execute(context)`. Each middleware calls `next()` to pass to the next one. Returns the final context.

Problem Statement

Implement a `createPipeline()` that has `use(fn)` and `execute(context)`. Each middleware calls `next()` to pass to the next one. Returns the final context.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Middleware functions have signature: (context, next) => { ... }. šŸ’” Hint 2: Compose middleware by chaining next() calls. šŸ’” Hint 3: Iterate through middleware in order, each calling the next. āœ… Solution: ```javascript function createPipeline() { const middlewares = []; return { use(fn) { middlewares.push(fn); }, execute(context) { let index = 0; function next() { if (index < middlewares.length) { middlewares[index++](context, next); } } next(); return context; } }; } ```

Middleware Pattern

Medium
Implement a `createPipeline()` that has `use(fn)` and `execute(context)`. Each middleware calls `next()` to pass to the next one. Returns the final context.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"step1":true,"step2":true}