2.0.0 • Published 10 years ago

ready-mixin v2.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

ready-mixin

build status Coverage Status Dependency status Dev Dependency status

NPM js-standard-style

You have class with async constructor and you don't want use callback in constructor or make inheritance from some event emitter? Try ready-mixin!

Installation

npm install ready-mixin

API

_ready(err, ...)

Resolve or reject ready promise.

ready()

Return ready promise.

return: Promise

onReady(callback, opts)

  • function callback is node-style callback function
  • Object opts

isReady()

Return current ready status.

return: boolean

Examples

import { mixin } from 'core-decorators'
import ReadyMixin from 'ready-mixin'

@mixin(ReadyMixin)
class User {
  constructor (userId) {
    db.load(userId, (err, data) => {
      if (err === null) {
        this._data = data
      }

      this._ready(err)
    })
  }
}

let user = new User(0)
user.ready
  .then(() => {
    console.log('user was loaded!')
  }, (err) => {
    console.log('error on loading user!', err)
  })

spread in onReady

import { mixin } from 'core-decorators'
import ReadyMixin from 'ready-mixin'

@mixin(ReadyMixin)
class A {
  constructor () {
    setTimeout(() => { this._ready(null, 1, 2, 3) }, 1000)
  }
}

let a = new A()
a.onReady((err, v1, v2, v3) => {
  console.log(`Sum: ${v1 + v2 + v3}`) // Sum: 6
}, {spread: true})

License

Code released under the MIT license.

2.0.0

10 years ago

1.0.0

10 years ago

0.1.0

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago