Skip to content
This repository was archived by the owner on May 20, 2021. It is now read-only.

Speedup-lib/nodejs-method-chainer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaScript/TypeScript method chainer

This repository is no longer maintained. Please use the newer version.

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

About

Chain callbacks and promises together to handle the most complex flows

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published