The `composition` package provides a utility for composing asynchronous middleware functions in a manner similar to Koa. It uniquely supports mixing both traditional generator functions (using `yield next`) and modern `async/await` functions within the same middleware stack. Each composed middleware function is expected to return a Promise or handle `next()` correctly to ensure the control flow propagates. The package is currently at version 2.3.0, indicating a stable release focused on robust async control flow. While no explicit release cadence is documented, utilities of this nature tend to be stable with updates driven by significant changes in JavaScript's asynchronous patterns or bug fixes. Its key differentiator is the seamless interoperability between generator-based and promise-based async middleware, making it suitable for projects transitioning or requiring compatibility across different asynchronous programming styles.
npm install compositionVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to compose a stack of mixed generator, promise-returning, and async/await middleware functions, illustrating the control flow, context passing, and handling of return values and errors.
For `async/await` middleware, ensure `await next();` is used. For generator middleware, use `yield next;`. For plain promise-returning functions, ensure `return next().then(...)` or `return Promise.resolve().then(() => next())` is used to propagate control.
Use Node.js v7.6.0 or higher for native `async/await` support. For environments with limited feature support, transpile code using Babel or a similar tool to ensure compatibility with your target runtime.
Always pass the desired `this` context to the composed function via `fn.call(yourContext)` or `fn.apply(yourContext)`. If no specific context is required, passing `null` or `{}` is acceptable.Review all middleware functions to ensure `next` is correctly invoked and awaited/yielded. Verify that the function signature includes `next` as a parameter if it's being called.
Add a `.catch()` handler to the invocation of the composed function (e.g., `fn().catch(err => console.error(err))`) to gracefully handle any errors that propagate out of the middleware stack.
Ensure `await` is only used inside functions declared with the `async` keyword. If using `async/await` in older Node.js versions, ensure you are using Node.js v7.6.0 or newer.
No dependency data recorded yet.