Created
October 27, 2015 07:45
-
-
Save gartenfeld/ecce4f087c1539fb8d78 to your computer and use it in GitHub Desktop.
Async mapping.
This file contains hidden or 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
var asyncMap = function(tasks, finalCb){ | |
var results = []; | |
var completed = 0; | |
var checkCompleteness = function() { | |
if (completed === tasks.length) { | |
finalCb(results); | |
} | |
}; | |
var runTask = function (i) { | |
var onResult = function(val) { | |
results[i] = val; | |
completed++; | |
checkCompleteness(); | |
}; | |
tasks[i](onResult); | |
} | |
for(var i = 0; i < tasks.length; i++) { | |
// make `i` available in the closure scope | |
// so it can be accessed by `onResult` | |
runTask(i); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment