jobs-queues
Plugin that gives you sequential queues for your jobs as Array-like Objects in Express style
Installation
npm i jobs-queues
Test
npm test
Usage
const jobsQueues = require( 'jobs-queues' );
const queue = jobsQueues();
queue.push(
( finish ) => {
setTimeout( () => {
console.log( 'First job finished' );
finish( true, 'Hello world!' )
}, 2500 );
},
( finish, empty, ...results ) => {
if ( results[0] ) {
finish( results[1] );
} else {
empty();
}
console.log( 'Second job finished' );
},
async ( finish, empty, result ) => {
await new Promise( r => setTimeout( r, 500 ) );
console.log( result );
finish();
}
);
queue.push(
( finish ) => {
console.log( 'Another job list' );
finish();
}
);
queue.onError( ( err, refs ) => {
console.log( { err, refs } );
} );
Constructor
jobsQueues( started = true [, ...jobList: Function] );
Parameters
started
Default true
- Set to false
if you want to start your jobs laterjobList
Optional - Any function that accepts three parameters:
finish
Required - A callback you have to call at the end of every job. It accepts ...results
and pass them to the next job in the same job listempty
Optional - A callback you have to call to stop the jobs in the same job list...results
Optional - Any result yo have passed in the finish()
of the previous one job in the same job list
Return
A JobsQueues
instance that extends Array
Methods
push
queue.push( ...jobList );
Return
The index of the job list as Integer
start
queue.start();
onError
queue.onError( callback );
Parameters
callback
Required - A function that accepts error
and referements
of the interrupted job list index and job index (in the order you've pushed them)
Note
A job list is not directly related with errors of another. It runs in anyway when the previous one exits.