npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

monasync

v0.3.4

Published

Async/Sync Composition library for sync and async behaviors through an async monad to simplify program structure and comprehension

Downloads

1

Readme

Monasync

A composition library for simple, straightforward serialized and parallelized processes blending synchronous and asynchronous behaviors.

Serial and parallel composition bindings

monasync.binding.serial

Serial composition is done with monasync.binding.serial which has the signature, function+ => function, [function] => [*+] => undefined. It's simple to compose functions together this way:

    var serialComposition = monasync.binding.serial(
        asyncFn1,
        asyncFn2,
        asyncFn3
    );

    serialComposition(success, fail)(arg1, arg2, arg3);

    // Composed functions are also composable:

    var composedComposition = monasync.binding.serial(
        serialComposition,
        asyncFn4,
        asyncFn5
    );

monasync.binding.parallel

Parallel executing behaviors is done with monasync.binding.parallel which has the signature, function+ => function, [function] => [*+] => undefined. It's simple to compose functions together this way:

    var parallelComposition = monasync.binding.parallel(
        asyncTypedFn1,
        asyncTypedFn2,
        asyncTypedFn3
    );

    parallelComposition(success, fail)(arg1, arg2, arg3);

    // Binding multiple parallel behaviors together is simple:

    var composedComposition = monasync.binding.parallel(
        serialComposition,
        asyncTypedFn4,
        asyncTypedFn5
    );

Async typed function

The async typed function is simply a function which has the following signature, function, [function] => [*+] => undefined where the first function is a success function, the second, optional, function is a failure function.

This means, the async typed function accepts a function and an optional second function, returns a function which takes any number of optional arguments and returns undefined. A standard call example is like the following:

    myAsyncBehavior(success, failure)(arg1, arg2);

Asynchronous functions

It's common in Node and client-side Javascript to write functions which take callbacks as a final argument. Monasync has a function to do a function type-cast to convert your standard callback accepting function into an composable async typed function. Monatype does this by decorating your function, but you can easily write async typed functions yourself if you like.

monasync.async.wrap

Wrap is the method for wrapping asynchronous, callback-accepting functions in an async function type where the signature is as follows: function => function, [function] => [*+] => undefined. An example is as follows:

    function myAsynchronousFn (callback) {};

    var myNewFn = monasync.async.wrap(myAsynchronousFn);
    myNewFn(success, failure)(arg1, arg2);

monasync.async.serialize

Serialize returns an async-typed function which serializes a set of callback-accepting asynchronous functions. The signature for serialize is function+ => function, [function] => [*+] => undefined. This is the analog to binding.serial.

    function myAsyncFunction (callback) { /* does stuff and calls back */ };

    var myNewFunction = monasync.async.serialize(
        myAsyncFunction,
        myAsyncFunction,
        myAsyncFunction,
        myAsyncFunction
    );

    myNewFunction(success, failure)(arg1, arg2);

monasync.async.parallelize

Serialize returns an async-typed function which parallelizes a set of callback-accepting asynchronous functions. The signature for parallelize is function+ => function, [function] => [*+] => undefined. This is the analog to binding.parallel.

    function myAsyncFunction (callback) { /* does stuff and calls back */ };

    var myNewFunction = monasync.async.parallelize(
        myAsyncFunction,
        myAsyncFunction,
        myAsyncFunction,
        myAsyncFunction
    );

    myNewFunction(success, failure)(arg1, arg2);

Partial application on Async typed functions

It's possible to partially apply arguments to async typed functions with the partial method. Partial will apply arguments and return a new async typed function.

    var asyncAdd = monasync.sync.wrap(function (a, b) { return a + b; });
    var asyncAdd3 = asyncAdd.partial(3);

    asyncAdd3(function (value){
        console.log(value); // 11
    })(8);

Synchronous functions

Functions which are simple, data-in, data-out type behaviors can be wrapped to conform to the async function type. They can also be converted to a simple asynchronous function which accepts a callback. These behaviors are detailed below.

monasync.sync.toAsync

This wraps a synchronous function in a simple callback-accepting wrapper. This allows sync and async behaviors to play together more directly. The signature for this function looks like this: function => [*+], function => undefined. The toAsync function is used as follows:

    function myFunction (a, b) { /* do stuff and return */ }

    var myAsyncFunction = monasync.sync.toAsync(myFunction);
    myAsyncFunction(arg1, arg2, callback);

monasync.sync.wrap

This wraps a synchronous function in an async typed function. This allows sync and async behaviors to play together more directly. The signature for this function looks like this: function => function, [function] => [*+] => undefined. The toAsync function is used as follows:

    function myFunction (a, b) { /* do stuff and return */ }

    var myAsyncFunction = monasync.sync.toAsync(myFunction);
    myAsyncFunction(arg1, arg2, callback);

Putting it all together

Here's an example of using multiple monasync functions together to create a complex composite behavior:

    var parallelComposition = monasync.binding.parallel(
        asyncTypedFn,
        monasync.async.wrap(asyncFn),
    );

    var serialComposition = monasync.binding.serial(
        parallelComposition,
        asyncTypedFn1,
        monasync.sync.wrap(intermediateTransformation),
        asyncTypedFn2
    );

    serialComposition(success, fail)(arg1, arg2);