@speedup/method-chainer
Chain your JavaScript/TypeScript callbacks/promises together to handle the most complex workflows in a functional way.
Last updated a month ago by dmanavi .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @speedup/method-chainer 
SYNC missed versions from official npm registry.

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').default;

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

Current Tags

  • 1.0.3                                ...           latest (a month ago)

4 Versions

  • 1.0.3                                ...           a month ago
  • 1.0.2                                ...           a month ago
  • 1.0.1                                ...           a month ago
  • 1.0.0                                ...           a month ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (11)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |