{"library":"ready-callback","title":"Ready Callback for Asynchronous Task Coordination","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install ready-callback"],"cli":null},"imports":["import ready from 'ready-callback';\nconst appReady = ready();","import { Ready } from 'ready-callback';\nconst appReady = new Ready();","app.readyCallback('serviceName');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Koa from 'koa';\nimport ready from 'ready-callback';\n\nconst app = new Koa();\nconst appReady = ready();\n\n// Mix the ready-callback functionality into your Koa application instance\nappReady.mixin(app);\n\n// Register an asynchronous service task\nconst dbConnectDone = app.readyCallback('databaseConnection');\nsetTimeout(() => {\n  console.log('Database connected!');\n  dbConnectDone(); // Mark the task as complete\n}, 1000);\n\n// Register another async task, e.g., loading config\nconst configLoadDone = app.readyCallback('configurationLoading', { timeout: 500 });\nsetTimeout(() => {\n  console.log('Configuration loaded!');\n  configLoadDone();\n}, 300);\n\n// The main application logic waits for all registered tasks\napp.ready(() => {\n  console.log('All async tasks are ready. Launching server...');\n  app.listen(3000, () => {\n    console.log('Server listening on http://localhost:3000');\n  });\n});\n\n// Example of error handling for a task\napp.on('error', (err, ctx) => {\n  console.error('Server error or ready-callback error:', err.message);\n  // Handle errors emitted by ready-callback if a task fails and is not weak\n});\n\n// Example of timeout handling\napp.on('ready_timeout', (id) => {\n  console.warn(`Task '${id}' timed out!`);\n});","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}