GithubHelp home page GithubHelp logo

co-class's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.