Installations
npm install node-async-runner
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=14.0.0
Node Version
20.13.1
NPM Version
10.5.2
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
rstr74
Download Statistics
Total Downloads
2,988
Last Day
1
Last Week
5
Last Month
20
Last Year
228
GitHub Statistics
1 Stars
30 Commits
2 Watching
3 Branches
1 Contributors
Package Meta Information
Latest Version
2.0.0-rc.1
Package Id
node-async-runner@2.0.0-rc.1
Unpacked Size
13.75 kB
Size
4.43 kB
File Count
6
NPM Version
10.5.2
Node Version
20.13.1
Publised On
14 Sept 2024
Total Downloads
Cumulative downloads
Total Downloads
2,988
Last day
0%
1
Compared to previous day
Last week
66.7%
5
Compared to previous week
Last month
100%
20
Compared to previous month
Last year
55.1%
228
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No dependencies detected.
note:
This update of this package is in development, do not use in production
AsyncRunner v2.0.0
AsyncRunner is a Node.js utility for managing and executing asynchronous tasks with controlled concurrency. Version 2.0.0 introduces modern JavaScript features (async/await
), task hashing, task naming, and enhanced event handling.
Table of Contents
- Features
- Installation
- Usage
- Concurrency Control
- Task Execution Order
- API Reference
- Examples
- Version History
Features
- Asynchronous Task Management: Manage a queue of asynchronous tasks using
async/await
. - Controlled Concurrency: Limit the number of tasks running concurrently.
- Task Hashing: Automatically assign a unique hash to each task based on its function code.
- Task Naming: Assign names to tasks for better identification.
- Event Emission: Emit events (
'next'
,'done'
,'error'
) to track task execution. - Result Collection: Collect results and errors, maintaining the order of tasks added.
- Error Handling: Optionally stop execution upon encountering an error.
Installation
You can install AsyncRunner via npm:
1npm install async-runner
Or add the async-runner.js
file to your project.
Usage
Basic Usage
1const AsyncRunner = require('async-runner'); 2 3const runner = new AsyncRunner({ maxThreads: 5, stopOnError: false }); 4 5runner.add([ 6 async function () { 7 // Your async code here 8 }, 9 // ... more tasks 10]); 11 12runner.run().then((results) => { 13 console.log('Results:', results); 14});
Adding Tasks
Tasks can be added as functions or as objects with a task
function and an optional name
:
1// Adding a single task function 2runner.add(async function () { 3 // Task code 4}); 5 6// Adding tasks with names 7runner.add([ 8 { 9 task: async function () { 10 // Task code 11 }, 12 name: 'Task One', 13 }, 14 { 15 task: async function () { 16 // Task code 17 }, 18 name: 'Task Two', 19 }, 20]);
Event Handling
You can listen to various events emitted by the runner:
1runner.on('next', (taskFunction, taskHash, taskName) => { 2 console.log(`Starting task: ${taskName || 'Unnamed Task'}`); 3 console.log(`Task Hash: ${taskHash}`); 4}); 5 6runner.on('done', (results, errors) => { 7 console.log('All tasks completed.'); 8 console.log('Results:', results); 9 if (errors && errors.length > 0) { 10 console.log('Errors:', errors); 11 } 12}); 13 14runner.on('error', (err) => { 15 console.error('Execution halted due to error:', err); 16});
Concurrency Control
The maxThreads
option controls the maximum number of tasks that can run concurrently. Adjust it according to your needs:
1const runner = new AsyncRunner({ maxThreads: 3 });
- Note: Setting
maxThreads
to1
will execute tasks sequentially.
Task Execution Order
- Task Start Order: Tasks are started in the order they are added.
- Task Completion Order: Tasks may complete out of order due to their asynchronous nature.
- Result Storage Order: Results are stored in the order of tasks added, regardless of completion order.
API Reference
Constructor
1new AsyncRunner(options)
- options: An object with the following properties:
maxThreads
(number, default10
): Maximum number of concurrent tasks.stopOnError
(boolean, defaultfalse
): Stop execution upon encountering an error.
add(task)
Add tasks to the runner.
- task: A function, an array of functions, an object, or an array of objects. Each object can have:
task
(function): The task function to execute.name
(string, optional): A name for the task.
Example:
1runner.add(async function () { 2 // Task code 3}); 4 5runner.add([ 6 { 7 task: async function () { 8 // Task code 9 }, 10 name: 'Task Name', 11 }, 12]);
run()
Execute the tasks.
- Returns a
Promise
that resolves with the results array or rejects with an error ifstopOnError
istrue
.
Example:
1runner.run().then((results) => { 2 // Handle results 3}).catch((error) => { 4 // Handle error 5});
Events
-
'next': Emitted before a task starts execution.
- Listener Parameters:
taskFunction
(function): The task function.taskHash
(string): Unique hash of the task.taskName
(string or null): Name of the task.
- Listener Parameters:
-
'done': Emitted when all tasks have completed.
- Listener Parameters:
results
(array): Array of results from tasks.errors
(array): Array of errors (if any).
- Listener Parameters:
-
'error': Emitted when an error occurs and
stopOnError
istrue
.- Listener Parameters:
error
(Error): The error that occurred.
- Listener Parameters:
Example:
1runner.on('next', (taskFunction, taskHash, taskName) => { 2 // Handle event 3}); 4 5runner.on('done', (results, errors) => { 6 // Handle completion 7}); 8 9runner.on('error', (err) => { 10 // Handle error 11});
Examples
Simple Example
1const AsyncRunner = require('async-runner'); 2 3const runner = new AsyncRunner({ maxThreads: 2, stopOnError: false }); 4 5runner.add([ 6 async function () { 7 await new Promise((resolve) => setTimeout(resolve, 1000)); 8 return 'Result 1'; 9 }, 10 async function () { 11 await new Promise((resolve) => setTimeout(resolve, 500)); 12 return 'Result 2'; 13 }, 14]); 15 16runner.run().then((results) => { 17 console.log('Results:', results); 18});
Using Task Names and Hashes
1const AsyncRunner = require('async-runner'); 2 3const runner = new AsyncRunner({ maxThreads: 2, stopOnError: false }); 4 5runner.add([ 6 { 7 task: async function () { 8 await new Promise((resolve) => setTimeout(resolve, 1000)); 9 return 'Result 1'; 10 }, 11 name: 'Fetch Data', 12 }, 13 { 14 task: async function () { 15 await new Promise((resolve) => setTimeout(resolve, 500)); 16 return 'Result 2'; 17 }, 18 name: 'Process Data', 19 }, 20]); 21 22runner.on('next', (taskFunction, taskHash, taskName) => { 23 console.log(`Starting task: ${taskName}`); 24 console.log(`Task Hash: ${taskHash}`); 25}); 26 27runner.on('done', (results, errors) => { 28 console.log('All tasks completed.'); 29 console.log('Results:', results); 30}); 31 32runner.run();
Version History
- v2.0.0
- Rewritten using
async/await
for modern Node.js support. - Tasks are assigned unique hashes based on their function code.
- Tasks can be given names for identification.
- Added
'next'
event emitted before each task execution.
- Rewritten using
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENCE:0
- Info: FSF or OSI recognized license: MIT License: LICENCE:0
Reason
Found 0/15 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 17 are checked with a SAST tool
Score
3
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to node-async-runner
async-benchmark-runner
Benchmark runner for node focusing on measuring asynchronous code using promises.
@thi.ng/testament
Minimal, rational & TypeScript-friendly test runner, result export as CSV/JSON, watch mode, file fixtures
cynic
async testing framework for es-modules
bree
The best job scheduler for Node.js and JavaScript with cron, dates, ms, later, and human-friendly support. Works in Node v12.17.0+, uses worker threads to spawn sandboxed processes, and supports async/await, retries, throttling, concurrency, and cancelab