Created
February 28, 2015 21:52
-
-
Save beattyml1/49553670e81e4018cc82 to your computer and use it in GitHub Desktop.
First Attempt on an asynchronous function array handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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