command-executor
v2.0.2
Published
Simple Async Executor
Downloads
26
Readme
Async Executer
Simple Async Executor
Install
npm install --save command-executor
Setup
const { parallel, sequence } = require('command-executor');
Parallel
const result = yield parallel([
() => sleep(50).then(() => '[X]'),
() => Promise.reject(ex),
() => Promise.resolve('[Y]')
]);
t.same(result, ['[X]', ex, '[Y]']);
Sequence
const ex = new Error('[ERR]');
const commands = [
() => sleep(20).then(() => '[X]'),
() => Promise.reject(ex),
() => Promise.resolve('[Y]')
];
const result = yield parallel(commands);
t.same(result, ['[X]', ex]);
Combined
const ex = new Error('[First]');
const main1 = () => sequence([
() => Promise.resolve('[X]'),
() => Promise.reject(ex)
]);
const main2 = () => sequence([
() => Promise.reject(ex),
() => Promise.resolve('[Y]')
]);
const result = yield parallel([main1, main2]);
t.same(result, [['[X]', ex], [ex]]);
Async Executer for tasks
Setup
const { parallel, sequence } = require('command-executor').tasks;
Parallel
const result = yield parallel([{
id: 'Create Production',
command: () => Promise.resolve('[X]')
}, {
id: 'Create Stage',
command: () => Promise.reject(new Error('[Y]'))
}]);
t.same(result, [{
id: 'Create Production',
result: '[X]',
state: 'resolved'
}, {
id: 'Create Stage',
result: new Error('[Y]'),
state: 'rejected'
}]);
Sequent
const result = yield sequence([{
id: 'Create Production',
command: () => Promise.reject(new Error('[X]')
}, {
id: 'Create Stage',
command: () => Promise.resolve('[Y]'))
}]);
t.same(result, [{
id: 'Create Production',
result: new Error('[X]'),
state: 'rejected'
}, {
id: 'Create Stage',
result: undefined,
state: 'skipped'
}]);
Combined
const result = yield parallel([{
id: 'mainTask1',
command: () => sequence([
{ id: 'task1', command: () => Promise.resolve('[X]') },
{ id: 'task2', command: () => parallel([
{ id: 'subtask1', command: () => Promise.resolve('[H]'),
{ id: 'subtask2', command: () => Promise.resolve('[G]')
]) }
])
}, {
id: 'mainTask2',
command: () => sequence([
{ id: 'task3', command: () => Promise.reject(new Error('[V]')) },
{ id: 'task4', command: () => Promise.resolve('[Z]') }
])
}]);
t.same(result, [
{
id: 'mainTask1',
result: [
{ id: 'task1', result: '[X]', state: 'resolved' },
{
id: 'task2',
result: [
{ id: subtask1, result: '[H]', state: 'resolved' },
{ id: subtask2, result: '[G]', state: 'resolved' }
],
state: 'resolved'
}
],
state: 'resolved'
},
{
id: 'mainTask2',
result: [
{ id: 'task3', result: new Error('[V]'), state: 'rejected' },
{ id: 'task4', result: undefined, state: 'skipped' }
],
state: 'resolved'
}
]
);