Created
May 12, 2013 05:49
-
-
Save leafbird/5562569 to your computer and use it in GitHub Desktop.
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
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