Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
taskgroup
Advanced tools
Group together synchronous and asynchronous tasks and execute them with support for concurrency, naming, and nesting.
Group together synchronous and asynchronous tasks and execute them with support for concurrency, naming, and nesting.
require('taskgroup')
npm install --save taskgroup
require('taskgroup')
ender add taskgroup
require('taskgroup')
component install bevry/taskgroup
require('taskgroup')
bower install taskgroup
// Import
var TaskGroup = require('taskgroup').TaskGroup;
// Create our new group
var group = new TaskGroup();
// Define what should happen once the group has completed
group.once('complete', function(err,results){
// Log the error that has occured
console.log(err);
// => null
// Log the results that our group received from the executing items
console.log(JSON.stringify(results));
/* =>
[
[null, 'first', 'task'],
[null, 'second task'],
[null, [
[null, 'sub second task'],
[null, 'sub first', 'task']
]]
]
*/
});
// Add an asynchronous task that gives the result to the completion callback
group.addTask(function(complete){
setTimeout(function(){
complete(null, 'first', 'task');
},500);
});
// Add a synchronous task that returns the result
// Errors should be returned, though if an error is thrown we will catch it
group.addTask(function(){
return 'second task';
});
// Add a sub-group to our exiting group
group.addGroup(function(addGroup,addTask){
// Tell this sub-group to execute in parallel (all at once) by setting its concurrency to unlimited
// by default the concurrency for all groups is set to 1
// which means that they execute in serial fashion (one after the other, instead of all at once)
this.setConfig({concurrency:0});
// Add an asynchronous task that gives its result to the completion callback
addTask(function(complete){
setTimeout(function(){
complete(null, 'sub first', 'task');
},500);
});
// Add a synchronous task that returns its result
addTask(function(){
return 'sub second task';
});
});
// Execute our group
group.run();
new (require('taskgroup')).TaskGroup()
constructor(name?,fn?)
- create our new group, the arguments name
and fn
are optional, refer to their entries in configurationsetConfig(config)
- set the configuration for the group, returns chainaddTask(args...)
- create a new task item with the arguments and adds it to the group, returns the new task itemaddGroup(args...)
- create a new group item with the arguments and adds it to the group, returns the new group itemgetTotals()
- returns counts for the following {running,remaining,completed,total}
clear()
- remove the remaining items to be executedpause()
- pause the execution of the itemsstop()
- clear and pauseexit(err)
- stop and complete, err
if specified is sent to the completion event when firedcomplete()
- will fire the completion event if we are already complete, useful if you're binding your listeners after runrun()
- start/resume executing the items, returns chainname
, no default - allows us to assign a name to the group, useful for debuggingfn(addGroup,addTask,complete?)
, no default - allows us to use an inline and self-executing style for defining groups, useful for nestingconcurrency
, defaults to 1
- how many items shall we allow to be run at the same time, set to 0
to allow unlimitedpauseOnError
, defaults to true
- if an error occurs in one of our items, should we stop executing any remaining items?
false
will continue with execution with the other items even if an item experiences an errorrun()
- fired just before we execute the itemscomplete(err, results)
- fired when all our items have completedtask.run(task)
- fired just before a task item executestask.complete(task, err, args...)
- fired when a task item has completedgroup.run(group)
- fired just before a group item executesgroup.complete(group, err, results)
- fired when a group item has completeditem.run(item)
- fired just before an item executes (fired for both sub-tasks and sub-groups)item.complete(item, err, args...)
- fired when an item has completed (fired for both sub-task and sub-groups)new (require('taskgroup')).Task()
constructor(name?,fn?)
- create our new task, the arguments name
and fn
are optional though fn
must be set at some point, refer to their entries in configurationsetConfig(config)
- set the configuration for the group, returns chaincomplete()
- will fire the completion event if we are already complete, useful if you're binding your listeners after runrun()
- execute the taskname
, no default - allows us to assign a name to the group, useful for debuggingfn(complete?)
, no default - must be set at some point, it is the function to execute for the task, if it is asynchronous it should use the completion callback providedargs
, no default - an array of arguments that you would like to precede the completion callback when executing fn
run()
- fired just before we execute the taskcomplete(err, args...)
- fired when the task has completedThe biggest advantage and difference of TaskGroup over async.js is that TaskGroup has one uniform API to rule them all, whereas with async.js I found that I was always having to keep referring to the async manual to try and figure out which is the right call for my use case then somehow wrap my head around the async.js way of doing things (which more often than not I couldn't), whereas with TaskGroup I never have that problem as it is one consistent API for all the different use cases.
Let's take a look at what the most common async.js methods would look like in TaskGroup:
// ====================================
// Series
// Async
async.series([
function(){},
function(callback){callback();}
], next);
// TaskGroup
new TaskGroup().once('complete', next)
.addTask(function(){})
.addTask(function(callback){callback();})
.run();
// ====================================
// Parallel
// Async
async.parallel([
function(){},
function(callback){callback();}
], next);
// TaskGroup
new TaskGroup().setConfig({concurrency:0}).once('complete', next)
.addTask(function(){})
.addTask(function(callback){callback();})
.run();
// ====================================
// Map
// Async
async.map(['file1','file2','file3'], fs.stat, next);
// TaskGroup
var tasks = new TaskGroup().setConfig({concurrency:0}).once('complete', next);
['file1','file2','file3'].forEach(function(file){
tasks.addTask(function(complete){
fs.stat(file,complete);
});
});
tasks.run();
Another big advantage of TaskGroup over async.js is TaskGroup's ability to add tasks to the group once execution has already started - this is a common use case when creating an application that must perform it's actions serially, so using TaskGroup you can create a serial TaskGroup for the application, run it right away, then add the actions to the group as tasks.
A final big advantage of TaskGroup over async.js is TaskGroup's ability to do nested groups, this allowed us to created the Joe Testing Framework & Runner incredibly easily, and because of this functionality Joe will always know which test (task) is associated to which suite (task group), whereas test runners like mocha have to guess (they add the task to the last group, which may not always be the case! especially with dynamically created tests!).
You can discover the history inside the History.md file
Licensed under the incredibly permissive MIT License
Copyright © 2013+ Bevry Pty Ltd
Copyright © 2011-2012 Benjamin Arthur Lupton
v3.2.4 2013 October 27
FAQs
Group together synchronous and asynchronous tasks and execute them with support for concurrency, naming, and nesting.
The npm package taskgroup receives a total of 20,531 weekly downloads. As such, taskgroup popularity was classified as popular.
We found that taskgroup demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.