ready-callback is a utility library designed to manage the readiness of a server or application after a series of asynchronous tasks have completed. It's particularly useful in scenarios where a server needs to wait for database connections, external service initializations, or other long-running setup processes before it can start accepting requests. The current stable version is 4.0.0, released in October 2023. This package has a moderate release cadence, with major versions aligning with Node.js LTS updates. Key differentiators include its simple callback-based API for registering tasks, robust error handling with optional 'weak' dependencies, and configurable timeouts for individual tasks or the overall ready process. It also provides status updates during task completion. Originally used heavily within the Egg.js framework ecosystem, it offers a pragmatic solution for orchestrating application startup sequences.
npm install ready-callbackVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing `ready-callback` with Koa, registering multiple asynchronous tasks, handling their completion, and launching the server only when all tasks are complete, including error and timeout handling.
Upgrade your Node.js runtime environment to version 16.0.0 or higher to use ready-callback v4.0.0+.
Upgrade your Node.js runtime environment to version 14.0.0 or higher. For v4.0.0+, Node.js 16+ is required.
Implement robust error handling by listening to the 'error' event on the instance (`app.on('error', callback)`). If you want tasks to complete without blocking the ready process on error, mark them as weak dependencies: `app.readyCallback('service', {isWeakDep: true});` or initialize with `ready({isWeakDep: true});`.Listen for the `ready_timeout` event (`app.on('ready_timeout', (id) => { ... })`) to detect and log tasks that take too long. Adjust timeout values as appropriate for your service's startup characteristics.For the factory function, use `import ready from 'ready-callback';`. For the class constructor, use `import { Ready } from 'ready-callback';`. The package is fully TypeScript-refactored since v4.0.0.Upgrade your Node.js environment to version 16.0.0 or newer. You can use tools like `nvm` to manage Node.js versions.
Ensure you call `appReady.mixin(app);` after initializing `const appReady = ready();` and before calling `app.readyCallback()`.
For ESM, use `import ready from 'ready-callback';`. For CJS, use `const ready = require('ready-callback');` and then `const appReady = ready();`.Review your asynchronous task implementations to ensure that `done()` is always called, even in error conditions. Utilize timeouts (`{ timeout: ms }`) to detect hanging tasks, and consider `isWeakDep: true` for non-critical tasks.No dependency data recorded yet.