1.2.0 • Published 10 years ago
schedulerjs v1.2.0
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) → Sequenceadds a function to the queuedelay(Number number) → Sequenceadds 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.startedtriggered when the sequence was startedstoppedtriggered 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) → Scheduleradds a sequence to the parallel queueset(String name, Sequence seq) → Schedulersame 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 triggerederrorstartedtriggered when the scheduler was startedfinishedtriggered when all the sequences triggeredfinishedstoppedtriggered when the scheduler was stopped