New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

async-class

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-class

Cleaner ES6 async class methods

  • 0.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

async-class

Cleaner ES6 async class methods for Node 4.0.0+. A solution to using promises and coroutines with classes without the overhead of babel, or the need to adopt unimplemented syntax and features, until v8/node supports ES7 async/await.

Build Status

Installation

npm install --save async-class

Overview

Using only ES6 features, how would you achieve a class like the following?

'use strict';

class FakeDataStore {
  constructor() {
    this.store = new Map();
  }

  async setAsync(key, value) {
    this.store.set(key, value);
    return await Promise.resolve(key);
  }
}

You'd use libraries that offer coroutine functionality like co or bluebird. However, there's no way to decorate those methods with ES6. Without the ES6 class sugar, we'd like to achieve the following:

'use strict';
let Promise = require('bluebird');

function FakeDataStore {
  this.store = new Map();
}

FakeDataStore.prototype.setAsync = Promise.coroutine(function*(key, value) {
  this.store.set(key, value);
  return yield Promise.resolve(key);
};

That's where this library comes in. Using it is simple:

'use strict';
let wrap = require('async-class').wrap;

class FakeDataStore {
  constructor() {
    this.store = new Map();
  }

  *setAsync(key, value) {
    this.store.set(key, value);
    return yield Promise.resolve(key);
  }
}

module.exports = wrap(FakeDataStore);

Clean ES6 classes and async methods!

API

async-class.wrap(klass [, methodNames])

Wraps static and instance methods whose name ends with Async, or are GeneratorFunctions. Any GeneratorFunction is wrapped with bluebird.coroutine(), and others with bluebird.method(). Accepts an optional array of method names, wrapping only those found in the array, and disabling the Async suffix check. Returns the class.

async-class.wrapStaticMethods(klass [, methodNames])

Wraps static methods whose name ends with Async or are GeneratorFunctions. Any GeneratorFunction is wrapped with bluebird.coroutine(), and others with bluebird.method(). Accepts an optional array of method names, wrapping only those found in the array, and disabling the Async suffix check. Returns the class.

async-class.wrapInstanceMethods(klass [, methodNames])

Wraps instance methods whose name ends with Async, or are GeneratorFunctions. Any GeneratorFunction is wrapped with bluebird.coroutine(), and others with bluebird.method(). Accepts an optional array of method names, wrapping only those found in the array, and disabling the Async suffix check. Returns the class.

Keywords

FAQs

Package last updated on 02 Dec 2015

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc