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

tesk

v1.0.2

Published

A powerful and simple module to execute synchronous tasks or asynchronous tasks.

Downloads

20

Readme

Tesk

It's a powerful and simple node module of high order functions to execute synchronous tasks or asynchronous tasks.

Installation

$ npm install --save tesk

Quick usage

const tesk = require('tesk').tesk;
const result = new Array();

tesk()
    .do((task) => {
        console.log('Do something 1');
	    result.push(1);
	
	task.next(); // Go to next task
    })
    .do((task) => {
        console.log('Do something 2');
        result.push(2);

        task.next(); // Go to next task
    })
    .exec((err) => {
        if (err) {
            console.log(err);
        }
        else {
            console.log('All tasks finished!');
            console.log('Result expected: [1, 2]');
            console.log('Result:', result);
        }
    });

Guide

do(callback) - sync usage

Do Sync it's a list of tasks that you can create to execute in sequence one by one. When the last task was finished, the tesk go to callback in exec function.

const tesk = require('tesk').tesk;
const result = new Array();

tesk()
    .do((task) => {
        console.log('Do something 1');

        // Simulating a asynchronous task like a database query
        setTimeout(() => {
            result.push(1);

            task.next(); // Go to next task
        }, 2000);
    })
    .do((task) => {
        console.log('Do something 2');
        result.push(2);

        task.next(); // Go to next
    })
    .do((task) => {
        console.log('Do something 3');
        result.push(3);

        task.next(); // Go to next task
    })
    .exec((err) => {
        if (err) {
            console.log(err);
        }
        else {
            console.log('All tasks finished!');
            console.log('Result expected: [1, 2, 3]');
            console.log('Result:', result);
        }
    });

do(callback) - async usage

Do Async it's a list of tasks that you can create to execute without order. When all tasks was finished, the tesk go to callback in exec function. All tasks will be executed at same time (but not in parallel like a thread).

const tesk = require('tesk').tesk;
const result = new Array();

tesk()
    .do((task) => {
        console.log('Do something 1');

        // Simulating a asynchronous task like a database query
        setTimeout(() => {
            result.push(1);

            task.next(); // Go to next task
        }, 2000);
    })
    .do((task) => {
        console.log('Do something 2');
        result.push(2);

        task.next(); // Go to next task
    })
    .do((task) => {
        console.log('Do something 3');
        result.push(3);

        task.next(); // Go to next task
    })
    .execAsync((err) => {
        if (err) {
            console.log(err);
        }
        else {
            console.log('All tasks finished!');
            console.log('Result expected: [2, 3, 1]');
            console.log('Result:', result);
	}
    });

forEach(array, callback) - sync usage

ForEach sync is for iterate an array in sequence item by item and wait the current task finish to go to next item on array. When all tasks was finished, the tesk go to callback in exec function.

const tesk = require('tesk').tesk;
const result = new Array();

tesk()
    .forEach([1, 2, 3, 4], (elem, index, task) => {
        if (index == 2) {
            setTimeout(() => {
                result.push(elem);
                task.next(); // Go to next task/item
            }, 1);
        }
        else {
            result.push(elem);
            task.next(); // Go to next task/item
        }
    })
    .exec((err) => {
        if (err) {
	        console.log(err);
	    }
        else {
            console.log('All tasks finished!');
            console.log('Result expected: [1, 2, 3, 4]');
            console.log('Result:', result);
        }
    });

forEach(array, callback) - async usage

ForEach async is for iterate an array without order. When all tasks was finished, the tesk go to callback in exec function. All tasks will be executed at same time (but not in parallel like a thread).

const tesk = require('tesk').tesk;
const result = '';

tesk()
    .forEach([1, 2, 3, 4], (elem, index, task) => {
        if (index == 2) {
            setTimeout(() => {
                result += elem;
                task.next(); // Go to next task/item
            }, 1);
        }
        else {
            result += elem;
            task.next();
        }
    })
    .execAsync((err) => {
    	if (err) {
	        console.log(err);
        }
        else {
            console.log('All tasks finished!');
            console.log('Result expected: [1, 2, 4, 3]');
            console.log('Result:', result);
        }
    });

Accept a task

To accept a task just call the method next() from parameter task received on callback function. Error is null if all tasks was accepted.

tesk()
    .do((task) => {
	    task.next(); // Go to next task
    })
    .exec((err) => {
        if (err) {
	        console.log(err);
        }
        else {
            console.log('All tasks finished!');
        }
    });

Reject a task

To reject a task just call the method reject() from parameter task received on callback function. When a task was rejected all tasks will be canceled and the callback function receive a personalized argument error.

tesk()
    .do((task) => {
	    task.reject('Error on execute query'); // Reject task and send a personalized error
    })
    .exec((err) => {
    	if (err) {
	        console.log(err);
        }
        else {
            console.log('All tasks finished!');
        }
    });

Another way

Exist another way to use. Tesk have a finally and catch function. If you use finally and catch, you don't use a callback argument on exec() function

tesk()
    .do((task) => {
        console.log('Do something 1');
        
        task.next();
    })
    .do((task) => {
        console.log('Do something 2');
        
        task.next();
    })
    .finally(() => {
	    console.log('All tasks finished!');
    })
    .catch((err) => {
    	console.log('Errors', err);
    })
    .exec();

What not to do

Never use do() and forEach() together. It's doesn't work.

tesk()
    .do((task) => {
	    task.reject('Error on execute query'); // Reject task and send a personalized error
    })
    .forEach([1, 2, 3, 4], (elem, index, task) => {
        console.log(elem, index);
        task.next(); // Go to next task/item
    })
    .exec((err) => {
    	if (err) {
	        console.log(err);
        }
        else {
            console.log('All tasks finished!');
        }
    });

If you need use do() and forEach() you can just call tesk() again.

tesk()
    .do((task) => {
	task.reject('Error on execute query'); // Reject task and send a personalized error
    })
    .exec((err) => {
    	if (err) {
	        console.log(err);
        }
        else {
            console.log('Starting a forEach() tasks');
            
            tesk()
                .forEach([1, 2, 3, 4], (elem, index, task) => {
                    if (index == 2) {
                        setTimeout(() => {
                            result += elem;
                            task.next(); // Go to next task/item
                        }, 1);
                    }
                    else {
                        result += elem;
                        task.next();
                    }
                })
                .execAsync((err) => {
                    if (err) {
                        console.log(err);
                    }
                    else {
                        console.log('All tasks finished!');
                        console.log('Result expected: [1, 2, 4, 3]');
                        console.log('Result:', result);
                    }
                });
        }
    });

Tests

To run the test suite, first install the dependencies, then run npm run test:

$ npm install
$ npm run test

Coming soon

  • tesk.try() - Like a promise.race()
  • exec() or execAsync() - Receive in callback result array of all tasks. Then you will can use task.next(12), and when all tasks was finished you can receive error and result parameters. Result will be equals an array [12]

Related projects

async

License

MIT