Async utilities for the browser.
This library relies heavily on promises so for older browsers that do not support native promises use a polyfill. Suggestion: https://github.com/taylorhakes/promise-polyfill
npm install
jake
jake test
Install it form bower:
bower install lexm-async-utils
// loop until the index becomes 10 and then output it
var index = 0;
AsyncUtils.Loop.until(function() {
index++;
return index == 10;
}).then(function() {
console.log(index);
});
// async incrementing index
var index = 0;
AsyncUtils.Loop.unique('inc').until(function() {
index ++;
return index === 10;
}).then(function() {
console.log(index)
}).catch(function(e) {
console.error(e);
});
// this will cancel the previous job and create a new one
AsyncUtils.Loop.unique('inc').until(function() {
index ++;
});
var condition = false;
AsyncUtils.Loop.until(function(){
if(condition === true) {
this.done();
return;
}
try {
// Do something with that condition variable
} catch(e) {
this.terminate('Error', e.message);
}
}).then(function() {
// do something when the loop completes
}).catch(function() {
// do something when the loop fails
});
AsyncUtils.Loop.until(function() {
// do something
});
var thread = new AsyncUtils.Thread(function(arg1, arg2) {
// do something in thread
console.log(arguments);
return '>Result from execution>'
});
thread.start();
thread.exec(['arg1', 'arg2']).then(function(result) {
console.log(result);
}).catch(function(e) {
// this will be executed if the thread has an error or was terminated before completing
console.warn(e);
});
// if -> elseif -> ... -> elseif -> else
AsyncUtils.if(function() {
return myVar == 'var';
}).then(function() {
console.log('equals "var"');
}).else(function() {
console.log('is NOT equal to "var".');
console.log('check if it equals "var1"');
return AsyncUtils.if(function() {
return myVar == 'var1';
});
}).then(function() {
console.log('equals "var1"');
}).else(function() {
console.log('is NOT equal to "var1"');
console.log('check if it equals "var2"');
return AsyncUtils.if(function() {
return myVar == 'var2';
});
}).then(function() {
console.log('equals "var2"');
}).else(function() {
console.log('is NOT equal to "var2".');
console.log('equals something else');
});
var List = [
'item1',
'item2',
'item3',
'test1',
'test2',
'test3'
];
AsyncUtils.List(List).each(function(item, index) {
console.log(index + ': ' + item);
}).then(function() {
console.log('===> List.each');
console.log('loop ended');
});
AsyncUtils.List(List).filter(function(item) {
return /^test/.test(item);
}).then(function(newList) {
console.log('===> List.filter');
console.log(newList);
});
AsyncUtils.List(List).map(function(item, index) {
return {
key: index,
value: item
};
}).then(function(newList) {
console.log('===> List.map');
console.log(newList);
});
AsyncUtils.List(List).find(function(item, index) {
return item === 'test3';
}).then(function(match) {
console.log('===> List.find');
console.log(match);
});
AsyncUtils.List(List).find(function(item, index) {
return item === 'test5';
}).then(function(match) {
console.log('===> List.find');
console.log(match);
}).catch(function() {
console.log('===> List.find - not found');
});
var printSomething = AsyncUtils.async(function(something) {
console.log(something);
return 'executed';
});
printSomething('Hello, World').then(function(val) {
console.log(val);
});
console.log('this will be executed before `printSomething`');
var printSomething = AsyncUtils.threaded(function(something) {
var i = 0;
while(i <= 10000000000) {i++;} // this will not block the ui because it run inside a worker
console.log(something);
return 'executed in a thread';
});
printSomething('Hello, World').then(function(val) {
console.log(val);
});
console.log('this will be executed before `printSomething`');
Issue Reports, Feature Requests, and Pull Requests are wellcome.