# wait-for-decorator

> Decorate a class method to return and wait on a Promise on the class instance before executing

Latest version **0.1.9** (published 2016-03-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install wait-for-decorator
pnpm add wait-for-decorator
yarn add wait-for-decorator
bun add wait-for-decorator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.9 |
| Published | 2016-03-09 |
| First published | 2016-02-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4.1.1 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | Sam Gluck |
| Maintainers | sdgluck |
| Keywords | decorator, class, method, wait, for, promise, resolved, resolve, babel |

## Links

- npm: https://www.npmjs.com/package/wait-for-decorator
- Repository: https://github.com/sdgluck/wait-for-decorator
- Homepage: https://github.com/sdgluck/wait-for-decorator#readme
- Issues: https://github.com/sdgluck/wait-for-decorator/issues
- npm.io page: https://npm.io/package/wait-for-decorator

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.9 (latest) — 2016-03-09
- 0.1.8 — 2016-03-04
- 0.1.7 — 2016-03-04
- 0.0.7 — 2016-02-25
- 0.0.6 — 2016-02-25
- 0.0.5 — 2016-02-25
- 0.0.4 — 2016-02-25
- 0.0.3 — 2016-02-25
- 0.0.2 — 2016-02-25
- 0.0.1 — 2016-02-25

## README

# wait-for-decorator

> Decorate a class method to return and wait on a Promise on the class instance before executing

Made with ❤ at [@outlandish](http://www.twitter.com/outlandish)

<a href="http://badge.fury.io/js/wait-for-decorator"><img alt="npm version" src="https://badge.fury.io/js/wait-for-decorator.svg"></a>
<a href="https://travis-ci.org/sdgluck/wait-for-decorator"><img alt="CI build status" src="https://travis-ci.org/sdgluck/wait-for-decorator.svg"></a>
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
    
## Install

    npm install wait-for-decorator --save

## Usage

Wait on a single Promise:

    @waitFor('promiseName')
    
Wait on multiple Promises (array):

    @waitFor(['promiseName1', 'promiseName2'])

Wait on multiple Promises (arguments):

    @waitFor('promiseName1', 'promiseName2')

## Requirements

- Node >=4.1.1
- Babel >=5
- `babel-plugin-transform-decorators-legacy` (Babel 6 only) ([npm](https://www.npmjs.com/package/babel-plugin-transform-decorators-legacy))

## @waitFor

If you have a class that performs some asynchronous operation during initialisation but don't want consumption of its methods to be hindered by forcing their invocation to happen _after_ initialisation is complete, you will do something like this:

    class Example {
      constructor () {
          this.promise = initialise()
      }
      someMethod () {
          // Ensure init is complete internally...
          return this.promise.then(() => {
              // ...and now actually do the 'someMethod' operation
          })
      }
      anotherMethod () {
          return this.promise.then(() => {
              // Do 'anotherMethod' operation
          })
      }
    }
    
Which gets ugly when you have lots of methods implementing this same behaviour. Instead, decorate the method using `wait-for-decorator`:

    @waitFor('promise')
    someMethod () {
        // Everything here will run after `this.promise` resolves
    }

    @waitFor('promise')
    anotherMethod () {
        // And everything here too!
    }

## Examples

__Wait for single Promise__

    class Say {
      constructor (message) {
        this.message = ''
        this.timeoutPromise = new Promise((resolve, reject) => {
          setTimeout(() => {
            this.message = message
            resolve()
          }, 50)
        })
      }

      @waitFor('timeoutPromise')
      sayMessage () {
        console.log(this.message)
      }
    }

    const inst = new Say('Hello, World!')
    inst.sayMessage() //=> 'Hello, World!'

__Wait for multiple Promises__

    class Say {
      constructor (messages) {
        this.message = ''
        this.firstPromise = new Promise((resolve) => {
          setTimeout(() => {
            this.message += messages[0]
            resolve()
          }, 50)
        })
        this.secondPromise = new Promise((resolve) => {
          setTimeout(() => {
            this.message += messages[1]
            resolve()
          }, 100)
        })
      }

      @waitFor(['firstPromise', 'secondPromise'])
      sayMessage () {
        console.log(this.message)
      }
    }

    const inst = new Say(['Hello, ', 'World!'])
    inst.sayMessage() //=> 'Hello, World!'

## Contributing

All pull requests and issues welcome!

If you're not sure how, check out Kent C. Dodds' [great video tutorials on egghead.io](https://egghead.io/lessons/javascript-identifying-how-to-contribute-to-an-open-source-project-on-github)!

---
_Source: https://npm.io/package/wait-for-decorator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
