1.5.0 • Published 7 years ago

ready-tracker v1.5.0

Weekly downloads
1
License
ISC
Repository
-
Last release
7 years ago

ready-tracker

Track when a project is ready or not with on object properties

In simple terms:

const ReadyTracker = require('ready-tracker')

function X() {
  ReadyTracker(this)

  Math.random() > 0.5 ? this.setReady() : this.setFailed(new Error())
}


new X().ready()
  .then((x) => /* use x */)
  .catch((err) => /* handle Error */)

Of course that could be easily handled by setting a promise in the initialiser. So a more complex setup would be:

const ReadyTracker = require('ready-tracker')

function RPI(params) {
  
  if(!valid(params))
    throw new Error('validation error')

  ReadyTracker(this)

  asyncAction(params)
    .then((result) => /* handle result */ this.setReady())
    .catch((err) => /* recover ? */)
    .then((result) => /* handle result */ this.setReady())
    .catch((err) => /* clean up */ this.setFailed(new Error(/*error*/)))
}

let a = new RPI(invalidparams) // Throws error here

let b = new RPI(validParams)
b.ready()
  .then(() => console.log(b, "works"))
  .catch((err) => console.error(b, "failed", err))

Usage

ReadyTracker(Object)

Attach ReadyTracker to an Object

Object#setReady()

Passes anything waiting on ready call.

Object#setFailed(error)

Fails anything waiting on ready call.

1.5.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago