{"id":16506,"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.","status":"active","version":"0.6.0","language":"javascript","source_language":"en","source_url":"git://github.com/voxpelli/node-promised-retry","tags":["javascript","typescript"],"install":[{"cmd":"npm install promised-retry","lang":"bash","label":"npm"},{"cmd":"yarn add promised-retry","lang":"bash","label":"yarn"},{"cmd":"pnpm add promised-retry","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primary CommonJS import for Node.js environments. The package is explicitly CJS.","symbol":"Retry","correct":"const Retry = require('promised-retry')"},{"note":"Correct ESM import for consuming the CommonJS default export. Avoid named imports as the package does not export a named 'Retry' symbol.","wrong":"import { Retry } from 'promised-retry'","symbol":"Retry","correct":"import Retry from 'promised-retry'"},{"note":"TypeScript type import for configuring the Retry constructor. This is a type-only import.","wrong":"import { RetryOptions } from 'promised-retry'","symbol":"RetryOptions","correct":"import type { RetryOptions } from 'promised-retry'"}],"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."},"warnings":[{"fix":"Upgrade your Node.js runtime to version 16.0.0 or later to use `promised-retry@0.6.0` and newer versions.","message":"Version 0.6.0 introduces a breaking change by requiring Node.js version 16.0.0 or higher. Older Node.js versions are no longer supported.","severity":"breaking","affected_versions":">=0.6.0"},{"fix":"Upgrade your Node.js runtime to version 14.0.0 or later to use `promised-retry@0.5.x`.","message":"Version 0.5.0 introduced a breaking change by requiring Node.js version 14.x or higher. If you are on an older Node.js version, you must upgrade.","severity":"breaking","affected_versions":">=0.5.0 <0.6.0"},{"fix":"Upgrade your Node.js runtime to version 12.0.0 or later to use `promised-retry@0.4.x`.","message":"Version 0.4.0 introduced a breaking change by requiring Node.js version 12.x or higher. Earlier Node.js versions are not supported.","severity":"breaking","affected_versions":">=0.4.0 <0.5.0"},{"fix":"For ESM, use `import Retry from 'promised-retry';`. Do NOT use named imports like `import { Retry } from 'promised-retry';` as it will result in a runtime error.","message":"This package is explicitly CommonJS (`Module type: CJS` in README). While it can be imported in ESM projects, ensure you use the correct default import syntax to avoid `TypeError`s.","severity":"gotcha","affected_versions":">=0.4.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Change `import { Retry } from 'promised-retry';` to `import Retry from 'promised-retry';`.","cause":"Incorrect ESM import syntax for a CommonJS default export.","error":"TypeError: (0 , promised_retry_1.default) is not a constructor"},{"fix":"Use the default import syntax for ESM: `import Retry from 'promised-retry';`.","cause":"Attempting to import `Retry` as a named export from a CommonJS module that only provides a default export.","error":"TypeError: Retry is not a constructor (when using named import in ESM)"},{"fix":"Upgrade your Node.js runtime to version 16.0.0 or later. If using `nvm`, run `nvm install 16` and `nvm use 16` (or a newer version).","cause":"Running `promised-retry@0.6.0` or newer on an unsupported Node.js version.","error":"This package requires Node.js version 16.0.0 or higher."}],"ecosystem":"npm"}