wait.js

3.0.0 • Public • Published

Wait.js

🖇️ JavaScript library to easily delay and chain class functions.

Gemnasium GitHub license GitHub release GitHub issues

Installation

npm i wait.js --save

Usage

Plug wait.js into your class:

import Wait from 'wait.js'
 
class MyClass {
    // step-1. Instantiate Wait.js and return "this" from your constructor:
    constructor () {
        this.w = Wait()
        return this
    }
 
    // step-2. Encapsulate each chainable function into the .handle() function:
    foo () {
        return this.w.handle(this, () => {
            /* Code here */
        })
    }
    bar () {
        return this.w.handle(this, () => {
            /* Code here */
        })
    }
 
    // step-3. Add a .pause() feature to your class:
    wait (milliseconds) {
        return this.w.pause(this, milliseconds)
    }
}
 
// step-4. It just works! You can now chain and delay your class functions:
const c = new MyClass()
c.foo().wait(2000).bar().foo()

Async functions

Wait.js also provides the ability to deal with async functions, by manually triggering the next execution:

import Wait from 'wait.js'
 
class MyClass {
    //..
 
    async () {
        // step-1. Add a "done" parameter to the encapsulating function
        // step-2. Manually call .done() when your async function is done
        return this.w.handle(this, (done) => {
            done()
        })
    }
 
    //..
}
 
// step-3. It just works! You can now chain and delay async functions:
const c = new MyClass()
c.foo().wait(2000).async().bar()

Contribution

npm run watch
npm run test

License

MIT © 2017 Sylvain Simao

Package Sidebar

Install

npm i wait.js

Weekly Downloads

1

Version

3.0.0

License

MIT

Last publish

Collaborators

  • maoosi