This package has been deprecated

Author message:

This package is deprecated in favor of async package. Try installing async package from caolan.

@speedup/method-chainer
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

JavaScript/TypeScript method chainer

Chain your JavaScript/TypeScript callbacks/promises together to handle the most complex workflows in a functional way.

NPM version NPM downloads

Installation

# NPM
npm i @speedup/method-chainer --save

# Yarn
yarn install @speedup/method-chainer

Usage

JavaScript


const MethodChainer = require('@speedup/method-chainer');

const factory = new MethodChainer.ConductorFactory();

const adderAsync = (c) => async (n) => n + c;
const adderCallback = (c) => (n, callback) => callback(null, n + c);

factory
    .handle(MethodChainer.HandlerFactory.wrapAsyncMethod(adderAsync(2)))
    (MethodChainer.HandlerFactory.wrapCallbackMethod(adderCallback(2)));

const conductor = factory.toConductor();

conductor.run(2, (err, result) => {

    // result is equal to 6
});

conductor.runAsync(2)
    .then(result => {

        // result is equal to 6
    })
    .catch(err => { });

// inside an awaitable function
const result = await conductor.runAsync(2);
// result is equal to 6

TypeScript


import * as MethodChainer from '@speedup/method-chainer';

const factory = new MethodChainer.ConductorFactory();

// these are method factories
const adderAsync = (c: number) => async (n: number) => n + c;
const adderCallback = (c: number) => (n: number, callback: (err: any, result: number) => void): void => callback(null, n + c);

// add your flow here
factory
    .handle<number, number>(MethodChainer.HandlerFactory.wrapAsyncMethod(adderAsync(2)))
    <number>(MethodChainer.HandlerFactory.wrapCallbackMethod(adderCallback(2)));

// generate conductor
const conductor = factory.toConductor();

// run using callback
conductor.run(2, (err, result) => {

    // result is equal to 6
});

// run using promise
conductor.runAsync(2)
    .then(result => {

        // result is equal to 6
    })
    .catch(err => { });

// run using await keyword
// inside an awaitable function
const result = await conductor.runAsync(2);
// result is equal to 6

And you're good to go!

License

MIT

Package Sidebar

Install

npm i @speedup/method-chainer

Weekly Downloads

3

Version

1.0.5

License

MIT

Unpacked Size

33.1 kB

Total Files

17

Last publish

Collaborators

  • dmanavi