# co-class

> Async class methods with coroutines

Latest version **1.1.0** (published 2018-09-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install co-class
pnpm add co-class
yarn add co-class
bun add co-class
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2018-09-30 |
| First published | 2018-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 2 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Overlook Motel |
| Maintainers | overlookmotel |
| Keywords | coroutine, coroutines, class, async, await, generator, generators |

## Links

- npm: https://www.npmjs.com/package/co-class
- Repository: https://github.com/overlookmotel/co-class
- Homepage: https://github.com/overlookmotel/co-class#readme
- Issues: https://github.com/overlookmotel/co-class/issues
- npm.io page: https://npm.io/package/co-class

## Dependencies (2)

- [co-simple](https://npm.io/package/co-simple.md) ^1.0.1
- [is-generator](https://npm.io/package/is-generator.md) ^1.0.3

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2018-09-30
- 1.0.1 — 2018-07-29
- 1.0.0 — 2018-07-29

## README

# co-class.js

# Async class methods with coroutines

## Current status

[![NPM version](https://img.shields.io/npm/v/co-class.svg)](https://www.npmjs.com/package/co-class)
[![Build Status](https://img.shields.io/travis/overlookmotel/co-class/master.svg)](http://travis-ci.org/overlookmotel/co-class)
[![Dependency Status](https://img.shields.io/david/overlookmotel/co-class.svg)](https://david-dm.org/overlookmotel/co-class)
[![Dev dependency Status](https://img.shields.io/david/dev/overlookmotel/co-class.svg)](https://david-dm.org/overlookmotel/co-class)
[![Greenkeeper badge](https://badges.greenkeeper.io/overlookmotel/co-class.svg)](https://greenkeeper.io/)
[![Coverage Status](https://img.shields.io/coveralls/overlookmotel/co-class/master.svg)](https://coveralls.io/r/overlookmotel/co-class)

## What's it for?

In versions of Node without `async/await` support, co-routines can be used to implement the same functionality using generator functions.

But defining class methods as co-routines is a pain. This module solves that problem.

## Usage

If you're on a version of Node that doesn't support `async/await`, instead of this code:

```js
class MyClass {
  async doSomethingAsync(a, b, c) {
    const res = await Promise.resolve(a * b * c);
    return res;
  }
}
```

...write this instead:

```js
const coClass = require('co-class');

class MyClass {
  *doSomethingAsync(a, b, c) {
    const res = yield Promise.resolve(a * b * c);
    return res;
  }
}

coClass( MyClass );
```

### How it works

All class methods (both instance methods and static methods) which are generator functions are converted into co-routines.

Co-routines are equivalent to `async/await` syntax. Co-routine wrapping is performed using [co-simple](https://www.npmjs.com/package/co-simple).

`super` also works as you'd expect for calling methods on a super-class.

### Wrapping just instance or static methods

To wrap only static methods:

```js
coClass.static( MyClass );
```

To wrap on instance methods:

```js
coClass.instance( MyClass );
```

Calling `coClass( MyClass )` is equivalent to calling both the above methods.

### Using an alternative wrapper

Just provide it as an option:

```js
const co = require('co-bluebird');
coClass( myClass, { wrapper: co.wrap } );
```

## Tests

Use `npm test` to run the tests. Use `npm run cover` to check coverage.

## Changelog

See [changelog.md](https://github.com/overlookmotel/co-class/blob/master/changelog.md)

## Issues

If you discover a bug, please raise an issue on Github. https://github.com/overlookmotel/co-class/issues

## Contribution

Pull requests are very welcome. Please:

* ensure all tests pass before submitting PR
* add an entry to changelog
* add tests for new features
* document new functionality/API additions in README

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