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

qtools-asynchronous-pipe-plus

v1.0.4

Published

Asynchronous-pipe for the future. Includes taskListPlus and utility functions I have learned to like"

Downloads

2

Readme

qtools-asynchronous-pipe

asynchronousPipe() takes an array of asynchronous functions and executes them in sequence. As each completes, the next is called with the return result of the previous function.

The purpose of the package is to create a joint scope for a series of asynchronous operations.

There are two main functions:

const pipeRunner = asynchronousPipePlus.pipeRunner;
const taskListPlus = asynchronousPipePlus.taskListPlus;

It is executed like this:

const taskList = new taskListPlus();

taskList.push(someWorkListFunction);

pipeRunner(taskList.getList(), initialValue, [callback])

The parameters are:

taskList:         an array of functions (This can be a normal Array())
initialValue:    optional initial value passed to the first function
callback:        optional function called after all the other functions are finished

The workList functions must have the signature:

(args, next)={
    const result= "some functionOf(args), probably the result of an I/O function";
    next(err, result); //mandatory
}

taskListPlus() returns, in addition to push() and getList(), several functions to control the parameters passed between process steps. These are undocumented for now:

returnGlobalArgs, getArgsValue, enforceScope, reduceToLocalScope, restoreGlobalScope

There is another taskListPlus() function that I used like crazy back in the day whose function is also, for now, undocumented: addFromDelegate.

UPDATE 6/7/19

There is a special value of err, skipRestOfPipe, eg:

next('skipRestOfPipe', result);

This will do as the symbol says, skip the rest of the pipe. It serves the role of 'break' in loops. Practically speaking, it immediately calls the callback function with the current value of result with the error value of 'skipRestOfPipe'.

It is often good to have special code in the final callback to change a skipRestOfPipe error status for the next callback in case it's not an asynchronousPipe and treats it as an error.

Eg,

callback(err=='skipRestOfPipe'?'':err, result)

The optional callback function is called as:

callback(err, result);

pipeRunner() does not return anything.

UPDATE: v1.04 (6/6/24)

There are two new utility functions:

asynchronousPipePlus.mergeArgs
asynchronousPipePlus.forwardArgs

These fabricate a callback that makes the args object available to the rest of the process sequence.

mergeArgs is used when a result is expected. The result is added to the 'args' object using a string parameter as the property name:

const mergeArgs = (args, next, propertyName) => (err, result) =>
    next(err, {
        ...args,
        ...(propertyName ? { [propertyName]: result } : result)
    });

It is called like this:

taskList.push((args, next) => {
    const {argsForFunction}=args;
    asyncFuncWithCallback(
        argsForFunction,
        mergeArgs(args, next, 'propertyNameForResult')
    );
});


taskList.push((args, next) => {
    const {propertyNameForResult, otherArgIfAny}=args;

    // ...
});

forwardArgs is used when no result parameter is expected:

const forwardArgs = ({ next, args }) => err => next(err, args);

taskList.push((args, next) => {
    const {argsForFunction}=args;
    asyncFuncWithCallback(
        argsForFunction,
        forwardArgs({ next, args })
    );
});


taskList.push((args, next) => {
    const {otherArgIfAny}=args;

    // ...
});

===================================

Misc Example from test.js

#!/usr/local/bin/node 'use strict';

const asynchronousPipePlus = new require('qtools-asynchronous-pipe-plus')(); const pipeRunner = asynchronousPipePlus.pipeRunner; const taskListPlus = asynchronousPipePlus.taskListPlus;

const pipeFunction = (inData, callback) => { const taskList = new taskListPlus();

taskList.push((args, next) => {
    const localCallback = (err, localResult1) => {
        if (err == 'someParticularError') {
            next('skipRestOfPipe', err);
            return;
        }
        console.dir({ 'only inboundData so far': args });

        args.localResult1 = localResult1;
        next(err, args);
    };
    localCallback('', 'localResult1_value');
});


taskList.push((args, next) => {
    const localCallback = (err, localResult3) => {
        args.localResult3 = localResult3;
        next(err, args);
    };
    console.dir({ 'shows localResult1 and inboundData': args });
    localCallback('', 'localResult3_value');
});

taskList.push(
    (args, next) => {
        const localCallback = (err, localResult2) => {
            args.localResult2 = localResult2;
            next(err, args);
        };
        console.dir({ 'shows localResult1 only': args });
        localCallback('', 'localResult2_value');
    },
    ['localResult1']
);

taskList.push(taskList.restoreGlobalScope());

taskList.push((args, next) => {
    const localCallback = (err, localResult3) => {
        args.localResult3 = localResult3;
        next(err, args);
    };
    console.dir({ 'shows all incoming values, 1, 2, hiding test': args });
    localCallback('', 'localResult3_value');
});

taskList.push(
    (args, next) => {
        const localCallback = (err, localResult4) => {
            args.localResult4 = localResult4;
            next(err, args);
        };
        console.dir({ 'shows localResult1 and inboundData': args });
        localCallback('', 'localResult2_value');
    },
    ['localResult3', 'inboundData']
);

const initialData = typeof inData != 'undefined' ? inData : {};
pipeRunner(taskList.getList(), initialData, (err, finalResult) => {
    console.dir({ 'final: shows localResult3, inboundData, and localResult4': finalResult });
    callback(err, finalResult);
});

};

pipeFunction({ inboundData: 'inboundData_value' }, (err, result) => { console.log('if you do not see errors, it works'); });