Socket
Socket
Sign inDemoInstall

co-class

Package Overview
Dependencies
2
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    co-class

Async class methods with coroutines


Version published
Weekly downloads
1
decreased by-94.12%
Maintainers
1
Install size
17.7 kB
Created
Weekly downloads
 

Changelog

Source

1.1.0

  • wrapper option

Readme

Source

co-class.js

Async class methods with coroutines

Current status

NPM version Build Status Dependency Status Dev dependency Status Greenkeeper badge Coverage Status

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:

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

...write this instead:

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.

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:

coClass.static( MyClass );

To wrap on instance methods:

coClass.instance( MyClass );

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

Using an alternative wrapper

Just provide it as an option:

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

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

Keywords

FAQs

Last updated on 30 Sep 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc