schedulerjs
v1.2.0
Published
A promise execution flow control library
Downloads
11
Maintainers
Readme
scheduler.js
A wrapper around Promise to control the execution flow
License
Released under MIT License
Tests
Install mocha : npm install -g mocha
Start tests with : npm test
Sequence
Sequence will execute Promises in serie
Example :
let seq = new Sequence();
seq
.next(() => {
return promise;
})
.delay(500)
.next(() => {
return new Promise((resolve, reject) => {
resolve();
});
})
.start();
seq.on('started', () => {
console.log('Sequence started');
});
seq.on('next', (pos, total) => {
console.log(`${pos} / ${total}`);
});
seq.on('error', err => {
console.log(`A promise rejected with ${err}`);
});
seq.on('stopped', () => {
console.log('Sequence stopped');
});
seq.on('finished', () => {
console.log('Sequence finished');
});
API
constructor()
initializes the sequencenext(Function callback) → Sequence
adds a function to the queuedelay(Number number) → Sequence
adds a timeout function to the queuestop()
stops the sequencestart()
starts the sequence
Events
error(Object err)
triggered when a Promise rejectedfinished()
triggered when all the Promise were finishednext(Number position, Number total)
triggered when one promise finished.started
triggered when the sequence was startedstopped
triggered when the sequence was stopped
Scheduler
Scheduler will execute Sequences in parallel
Example :
let scheduler = new Scheduler();
let seq1 = new Sequence();
let seq2 = new Sequence();
seq1
.next(...)
.delay(500)
.next(...);
seq2
.next(...)
.next(...);
scheduler
.add(seq1)
.set('othername', seq2) // sets scheduler.othername to seq2
.start();
API
constructor()
initializes the scheduleradd(Sequence seq) → Scheduler
adds a sequence to the parallel queueset(String name, Sequence seq) → Scheduler
same as add, but sets the reference inside the schedulerstart()
starts all the sequencesstop()
stops all the sequences
Events
error(Object err)
triggered when one sequence triggerederror
started
triggered when the scheduler was startedfinished
triggered when all the sequences triggeredfinished
stopped
triggered when the scheduler was stopped