Skip to content

Instantly share code, notes, and snippets.

@beattyml1
Created February 28, 2015 21:52
Show Gist options
  • Save beattyml1/49553670e81e4018cc82 to your computer and use it in GitHub Desktop.
Save beattyml1/49553670e81e4018cc82 to your computer and use it in GitHub Desktop.
First Attempt on an asynchronous function array handling
module Async {
export function map<TIn, TOut>(input: Array<TIn>, processor: (item: TIn, after:(outputItem: TOut)=>void)=>void, returns: (output: Array<TOut>)=>void){
var results = [];
forEach(input, (item, index, checkIfDone)=>processor(item, function(outputItem){
results[index] = outputItem;
checkIfDone();
}), function(){
returns(results);
});
}
export function filter<T>(input: Array<T>, processor: (item: T, after: (include: boolean)=>void)=>void, returns: (output: Array<T>)=>void) {
var results = [];
forEach(input, (item, index, checkIfDone)=>processor(item, function(include){
if (include) {
results.push(item);
}
checkIfDone();
}), function(){
returns(results);
});
}
export function forEach<TItem>(array: Array<TItem>, processor:(item: TItem, index: number, checkIfDone: ()=>void)=>void, after: ()=>void) {
var returned = array.length;
array.forEach((item, index)=>processor(item, index, function() {
returned--;
if (returned == 0) {
after();
}
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment