{"library":"promised-retry","title":"Promised Retry","description":"Promised Retry is a lightweight, generic utility for implementing promise-based retry mechanisms. It is particularly useful for scenarios requiring robust connection handling, such as ensuring database or message queue availability, by automatically retrying failed operations. The current stable version is 0.6.0. The package has an irregular release cadence, often driven by updates to its minimum Node.js version requirement. Key differentiators include highly configurable retry delays (minimum, base, exponent, or a custom function), an optional retry limit, and lifecycle hooks for setup, successful attempts, and graceful shutdown. It also provides methods to explicitly reset or end the retry process. This library focuses purely on retry logic without imposing additional runtime dependencies.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install promised-retry"],"cli":null},"imports":["const Retry = require('promised-retry')","import Retry from 'promised-retry'","import type { RetryOptions } from 'promised-retry'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const Retry = require('promised-retry');\nconst { Client } = require('pg');\n\nconst dbConfig = {\n  user: process.env.DB_USER ?? 'testuser',\n  host: process.env.DB_HOST ?? 'localhost',\n  database: process.env.DB_NAME ?? 'testdb',\n  password: process.env.DB_PASSWORD ?? 'testpassword',\n  port: parseInt(process.env.DB_PORT ?? '5432', 10),\n};\n\nlet currentDbClient = null;\nconst channels = ['my_channel', 'another_channel'];\n\nconst retryInstance = new Retry({\n  name: 'PostgreSQL Connection',\n  try: async () => {\n    const db = new Client(dbConfig);\n    db.on('error', (err) => {\n      console.error('DB Client error, resetting retry:', err.message);\n      currentDbClient = null; // Mark client as invalid\n      retryInstance.reset();\n      // Optionally, if you want to immediately re-try after a connection error:\n      // retryInstance.try(); \n    });\n    await db.connect();\n    console.log('Database connected successfully!');\n    return db; // Return the connected client\n  },\n  success: db => {\n    currentDbClient = db;\n    db.on('notification', (msg) => {\n      console.log(`Received notification on channel ${msg.channel}: ${msg.payload}`);\n    });\n    channels.forEach(channel => {\n      db.query('LISTEN ' + channel);\n      console.log(`Listening on channel: ${channel}`);\n    });\n    console.log('Successfully established DB connection and listeners.');\n  },\n  end: async db => {\n    if (db) {\n      console.log('Ending database connection...');\n      await db.end();\n      console.log('Database connection ended.');\n    }\n  },\n  retryMin: 1000, // minimum 1 second delay\n  retryBase: 1.5, // exponential backoff\n  retryExponent: 5 // max exponent for delay calculation\n});\n\nasync function initializeDbConnection() {\n  try {\n    await retryInstance.try();\n    console.log('Initial database connection established.');\n  } catch (error) {\n    console.error('Failed to establish database connection after retries:', error.message);\n  }\n}\n\ninitializeDbConnection();\n\n// Example of how to end the retry mechanism (e.g., on application shutdown)\nprocess.on('SIGINT', async () => {\n  console.log('Received SIGINT. Shutting down...');\n  await retryInstance.end(currentDbClient);\n  process.exit(0);\n});\n","lang":"javascript","description":"Demonstrates basic usage of `promised-retry` to establish and maintain a PostgreSQL database connection with automatic retries, custom delay settings, and graceful shutdown handling. It uses environment variables for database configuration for better security and flexibility.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}