jake-synchronoustasks
v1.0.0
Published
run asynchronous tasks synchronously with Jake
Downloads
3
Readme
jake-synchronousTasks
run asynchronous tasks synchronously with Jake
Installation
yarn add jake-synchronousTasks
# or
npm install -S jake-synchronousTasks
Jake exemples
Basic exemple with sync and async task without jake-synchronousTasks
task('async1', {async: true}, () => {
setTimeout(() => {
console.log('async1');
complete();
}, 1000);
})
task('async2', {async: true}, () => {
setTimeout(() => {
console.log('async2');
complete();
}, 500);
})
task('sync', () => {
console.log('sync');
})
task('execAll', () => {
jake.Task['async1'].invoke();
jake.Task['async2'].invoke();
jake.Task['sync'].invoke();
console.log("Finish");
})
When execute execAll
result is :
sync
Finish
async2
async1
because Jake invoke all task asynchronely
Invoke synchronously without jake-synchronousTasks
If you want to execute these tasks synchronously without jake-synchronousTasks you must write :
task('execAll', () => {
const async1 = jake.Task['async1'];
const async2 = jake.Task['async2'];
const sync = jake.Task['sync'];
async1.addListener('complete', () => {
async2.invoke();
});
async2.addListener('complete', () => {
sync.invoke();
});
sync.addListener('complete', () => {
console.log("Finish");
});
async1.invoke();
})
When execute execAll
result is :
async1
async2
sync
finish
Use jake-synchronousTasks
const synchronousTasks = require("jake-synchronousTasks");
task('execAll', () => {
synchronousTasks([
{ task: jake.Task["async1"], exec: task => task.invoke() },
{ task: jake.Task["async2"], exec: task => task.invoke() },
{ task: jake.Task["sync"], exec: task => task.invoke() }
], () => console.log("Finish"));
// OR
// invoke task by default
synchronousTasks([
jake.Task["async1"],
jake.Task["async2"],
jake.Task["sync"]
], () => console.log("Finish"));
})
Usages
synchronousTasks([
{ task: jake.Task["async1"], exec: task => task.invoke("-v") }, // with parameter
{ task: jake.Task["async2"], exec: task => task.execute() }, // use execute instead of invoke
jake.Task["sync"] // invoke without parameter by default
], () => console.log("Finish"));