1.0.0 • Published 5 years ago

doneable v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

doneable

Easily extend a stateful callback based operation into a thenable that can await'ed.

npm install doneable

Usage

const Doneable = require('doneable')

class Operation extends Donable {
  constructor (args, callback) {
    super(callback)
    this.destroyed = false

    // do a bunch of stuff
    setImmediate(() => {
      if (this.destroyed) return this.done(new Error('Was aborted'))
      this.done(null, 'value')
    })
  }

  destroy () {
    this.destroyed = true
  }
}

const op = new Operation(..., function (err, val) {
  console.log('Optional callback', err, val)
})

// but you can also await it
console.log(await op) // returns 42

License

MIT

1.0.0

5 years ago