Skip to content

Instantly share code, notes, and snippets.

@leafbird
Created May 12, 2013 05:49
Show Gist options
  • Save leafbird/5562569 to your computer and use it in GitHub Desktop.
Save leafbird/5562569 to your computer and use it in GitHub Desktop.
function asyncMap( list, fn, cb_ ) {
var n = list.length
, result = []
, errState = null;
function cb (er, data) {
if (errState) return;
if (er) return cb_(errState = er);
results.push(data);
if (--n === 0) // 모든 리스트 처리 완료시
return cb_(null, results);
}
// action code
list.forEach(function (l) {
fn(l, cb);
});
}
function asyncMapOrder( list, fn, cb_ ) {
var n = list.length
, result = []
, errState = null;
function cbGen (i) {
return function cb (er, data) {
if (errState) return;
if (er) return cb_(errState = er);
results[i] = data;
if (--n === 0) // 모든 리스트 처리 완료시
return cb_(null, results);
}
}
// action code
list.forEach(function (l, i) {
fn(l, cbGen(i));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment