Promise Retry Utility (Bluebird-focused)

1.0.0 · abandoned · verified Sun Apr 19

The `retry-promise` package (version 1.0.0), last published in November 2015, provides a small utility function for automatically retrying promise-returning operations. It was explicitly designed to work with Bluebird Promises and implements a basic retry mechanism with configurable maximum attempts (`opts.max`) and a linear backoff strategy. The delay between retries is calculated as `attempt_number * opts.backoff`, meaning each subsequent retry waits proportionally longer. Given its age and lack of updates for nearly a decade, this package is considered abandoned. It lacks support for modern JavaScript features like native Promises or ES Modules and does not offer advanced retry strategies (e.g., jitter, custom error predicates) found in more actively maintained alternatives such as `p-retry` or `ts-retry-promise`. For new projects, developers should consider these contemporary libraries over `retry-promise`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `retry-promise` to retry a function that returns a Promise until it resolves. It includes a mock `User` service that simulates transient failures, showcasing the retry logic and linear backoff strategy.

const retry = require('retry-promise');

// Mock a User model that occasionally fails to demonstrate retries
let creationAttempts = 0;
const User = {
  forge: function(data) {
    return {
      createOrLoad: function() {
        return new Promise((resolve, reject) => {
          creationAttempts++;
          console.log(`[Mock User] Attempt ${creationAttempts} to create user ${data.email}`);
          if (creationAttempts < 3) { // Simulate transient failure for the first 2 attempts
            console.log(`[Mock User] Failed attempt ${creationAttempts}`);
            return reject(new Error('Database temporarily unavailable'));
          } else {
            console.log(`[Mock User] Succeeded on attempt ${creationAttempts}`);
            return resolve({ id: 1, email: data.email });
          }
        });
      }
    };
  }
};

// Mock Express-like req/next for a runnable example
const req = {};
const next = (err) => {
  if (err) {
    console.error('Middleware error:', err.message);
  } else {
    console.log('Middleware successful. User:', req.user);
  }
};

console.log('Starting retry operation...');
retry({ max: 3, backoff: 1000 }, function (attempt) {
  console.log(`retry-promise callback - Attempt ${attempt}`);
  return User
    .forge({ email: 'test@example.com' })
    .createOrLoad();
})
.then(function (user) {
  req.user = user;
  next();
})
.catch(function(error) {
  console.error('[retry-promise] Final failure:', error.message);
  next(error);
});

view raw JSON →