Medium
You need to implement a middleware system that processes requests in order and can handle both synchronous and asynchronous middleware. What's the correct implementation?
1class MiddlewareChain {2 constructor() {3 this.middlewares = [];4 }56 use(middleware) {7 this.middlewares.push(middleware);8 return this;9 }1011 // Missing implementation12 execute(context) {13 ____________________14 ____________________15 ____________________16 ____________________17 }18}1920// Usage:21const chain = new MiddlewareChain();2223chain.use(async (ctx, next) => {24 console.log('Start');25 ctx.start = Date.now();26 await next();27 console.log(`Time: ${Date.now() - ctx.start}ms`);28});2930chain.use(async (ctx, next) => {31 ctx.data = await fetchData();32 await next();33});3435const context = { params: { id: 1 } };36await chain.execute(context);