rewait
TypeScript icon, indicating that this package has built-in type declarations

2.0.0-rc.0 • Public • Published

Rewait

Version 2.0.0 License MIT Test Coverage 100%

A NodeJS library to wait for external resources to become available:

  • files
  • sockets
  • http/https
  • custom functions

For example, you may wish to wait for a database or message queue to become available before starting your HTTP server. Rewait does that.

Install

Easily install with the following NPM command, or download a release.

npm i rewait

Why use rewait?

  • No dependencies
  • Tiny (~400 logical lines of code)
  • 100% test coverage
  • Permissive FOSS license
  • Written in TypeScript
  • Used in major production evironments
  • Extensible

Usage

For detailed usage info, see the API documentation here.

Example

See more examples here.

const { retry, http, socket } = require('rewait')

retry(
  [
    http('http://localhost:3000'),
    http('https://localhost:3001/path/to/thing.txt'),
    socket('/var/run/app.sock'),
  ],
  {
    interval: 250, // check (at most) every 1/4 second
    timeout: 60000, // timeout after 60 seconds (on the dot)
  }
).then(() => {
  console.log('Ready!')
})

Custom checks

If you need custom functionality, you can easily write your own custom checks.

Simply throw an Error when "not ready".

Example:

function customCheck(options = {}) {
  return new Promise(function() {
    if (options.neverReady) {
      throw new Error('Never ready!')
    }
    if (options.waitUntil) {
      if (+new Date() < options.waitUntil) {
        throw new Error('Not ready!')
      }
    }
    // Ready, simply by not throwing
  })
}

Package Sidebar

Install

npm i rewait

Weekly Downloads

43

Version

2.0.0-rc.0

License

MIT

Unpacked Size

49.6 kB

Total Files

21

Last publish

Collaborators

  • jchook