
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
baby-workers
Advanced tools
Manage your functions asynchronously or as stack with baby-workers. You can also create worker for each element of an array or object (converted to array) they will be executed from the function.
Manage your functions asynchronously or as stack/timeout/interval with baby-workers. You can also create worker for a simple function or for each element of an array or any data they will be executed like worker in callback.
npm install --save baby-workers
Three entites :
The principe is to run asynchronouse (or not) function easily and manage them when the worker has finished with a callback. Launch any request on any element.
create(name: string, callback: function, data: any = undefined) : currentWorker {
Available: ALL,
Description: Create a new worker
}
run() : currentWorker {
Available: PARENT,
Description: Run current worker
}
stack() : currentWorker {
Available: PARENT,
Description: Run nodes like stack
}
timeout(time: number = 1) : currentWorker {
Available: PARENT,
Description: Run nodes like run in setTimeout
}
interval(time: number = 1000) : currentWorker {
Available: PARENT,
Description: Run nodes like run in setInterval
Additionnal functions:
stop() : currentWorker {
Available: NODE,
Description: Stop interval
}
}
getStatus() : string {
Available: ALL,
Description: Get status of current worker
}
getName() : string {
Available: ALL,
Description: Get name of current worker
}
pop() : currentWorker {
Available: NODE,
Description: Stop current node
}
addWorker() : currentWorker {
Available: ALL,
Description: Add virtual worker in current worker (it used for external asynch function)
}
removeWorker(isParent: boolean) : currentWorker {
Available: ALL,
Description: Remove virtual worker in current worker (it used for external asynch function)
}
complete(callback: function) : currentWorker {
Available: ALL,
Description: Call function when current process is finish (node, parent => when childrens are finish or root => when childrens are finish)
}
error(error: string, fatalError: string) : currentWorker {
Available: ALL,
Description: Set error in current worker and all parent in the tree
}
save(data: any) : currentWorker {
Available: ALL,
Description: Save any data in current worker (node, parent or root)
}
get() : any {
Available: ALL,
Description: Get data previously saved
}
root() : parentWorker {
Available: NODE,
Description: Get root/parent of current worker
}
parent(name: string) : parentWorker | nodeWorker {
Available: PARENT & NODE,
Description: Get any parent/node going up the tree
}
node(key: number) : nodeWorker {
Available: PARENT,
Description: Get direct node going down the tree
}
const babyWorkers = require('baby-workers');
const workers = new babyWorkers;
workers.create('basic', (worker, id) => {
setTimeout(() => {
console.log('basic =>', id);
worker.pop();
}, (~~(Math.random() * 1000)));
}, ['a', 'b', 'c', 'd']).run();
workers.basic.complete(() => {
console.log('All "basic" has finished');
});
workers.create('stack', (worker, id) => {
setTimeout(() => {
console.log('stack =>', id);
worker.pop();
}, (~~(Math.random() * 1000)));
}, ['z', 'y', 'x', 'w']).stack(); // mode stack enabled
workers.stack.complete(() => {
console.log('All "stack" has finished');
});
workers.create('simple', (worker, id) => {
setTimeout(() => {
console.log('simple =>', id);
worker.pop();
}, (~~(Math.random() * 1000)));
}, "toto").run();
workers.simple.complete(() => {
console.log('All "simple" has finished');
});
workers.simple.addWorker();
setTimeout(() => {
console.log('Okay now "simple" is complete');
workers.simple.removeWorker();
}, 2000);
workers.create('timeout', (worker) => {
console.log('Timeout called');
worker.pop();
}).timeout(1000);
workers.create('interval', (worker) => {
console.log('Interval called');
worker.save(worker.get() + 1);
worker.pop();
if (worker.get() == 5) {
workers.interval.stop();
}
}).interval(1000).save(0);
workers.complete(() => {
console.log('All "workers" has finished');
});
const request = require('request');
const babyWorkers = require('baby-workers');
class Main
{
constructor()
{
this.workers = new babyWorkers;
this.workers.create('users', this.getUser, ['58bdza054dre58a9dra56', 'ddz548ftbc5045dee87rr']).save([]).run();
this.workers.users.complete((error) => {
if (error !== null) {
return ;
}
console.log('Users:', this.workers.users.get());
});
this.workers.create('articles', this.getArticles, ['45dz54dez50dez84fzsd', 'bvc0b4b8fdfdg48dfgfd1', 'd48d4z0g4v8cx4q8sc4q']).save([]).run();
this.workers.articles.complete((error) => {
if (error !== null) {
return ;
}
console.log('Articles:', this.workers.articles.get());
});
this.workers.complete((error, fatalError) => {
if (error !== null) {
console.log(error);
console.error(fatalError);
return ;
}
console.log('All workers has finished');
});
}
getUser(worker, idUser)
{
request({
url: urlGetUser + idUser,
}, (error, response, body) => {
if (error !== null) {
worker.error('Get user ' + idUser + ' error', error);
return worker.pop();
}
worker.save(body); // to get idUser from getBillUser
worker.create('notifyUser', this.putNotifyUser, idUser).run();
worker.create('billUser', this.getBillUser, body.data.bills).save([]).run();
worker.billUser.complete(() => {
worker.root().save(worker.root().get().concat([{
firstName: body.data.firstName,
lastName: body.data.lastName,
bills: worker.billUser.get(),
}]));
worker.pop();
});
});
}
putNotifyUser(worker, idUser)
{
request({
url: urlNotifyUser + idUser,
}, (error, response, body) => {
if (error !== null) {
worker.error('Notify user ' + idUser + ' error', error);
}
worker.pop();
});
}
getBillUser(worker, idBill)
{
request({
url: urlGetBillUser + idBill,
}, (error, response, body) => {
if (error !== null) {
const idUser = worker.root().get().idUser; // previously saved from getUser
worker.error('Billd user ' + idUser + ' error', error);
} else {
worker.root().save(worker.root().get().concat([{
name: body.data.name,
amount: body.data.amount,
}]));
}
worker.pop();
});
}
getArticles(worker, idArticle)
{
request({
url: urlGetArticle + idArticle,
}, (error, response, body) => {
if (error !== null) {
worker.error('Article ' + idArticle + ' error', error);
} else {
worker.root().save(worker.root().get().concat([{
title: body.data.title,
content: body.data.content,
}]));
}
worker.pop();
});
}
}
module.exports = new Main;
FAQs
Execute and manage your code asynchronously with workers and promise.
The npm package baby-workers receives a total of 5 weekly downloads. As such, baby-workers popularity was classified as not popular.
We found that baby-workers demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.