when-ready

Increasing delays dynamically

Usage no npm install needed!

<script type="module">
  import whenReady from 'https://cdn.skypack.dev/when-ready';
</script>

README

when-ready

version status coverage dependencies devDependencies

Sugar way to manage your pendings.

Example

var Ready = require('..')

function Concat() {
  this.ready = Ready()
}

Concat.prototype.add = function(file) {
  this['addType' + (rand() % 2 + 1)](file)
}

Concat.prototype.addType1 = function(file) {
  var self = this

  this.ready.delay(
    new Promise(function (resolve) {
      setTimeout(function() {
        self.push('{' + file.slice(-1) + '}')
        resolve()
      }, rand())
    })
  )
}

Concat.prototype.addType2 = function(file) {
  var cb = this.ready.delay()

  var self = this
  setTimeout(function() {
    self.push('[' + file.slice(-1) + ']')
    cb()
  }, rand())
}

Concat.prototype.push = function(source) {
  console.log(source.toUpperCase())
}

Concat.prototype.end = function() {
  var self = this
  this.ready.then(function () {
    self.push('\n# end #')
  })
}

function rand() {
  return Math.ceil(Math.random() * 100)
}

var concat = new Concat()

concat.add('./a')
concat.add('./b')
concat.add('./c')
concat.end()

// [B]
// {C}
// [A]
// 
// # END #


class Ready()

Instances are thenable objects.

.then(onFulfilled, onRejected)

.catch(onRejected)

.delay(n)

If n is Number, it specifies the amount of time to be delayed.

If n is Promise, it delays until the promise resolves or rejects.

Otherwise, it delays until the returned callback is called.