Created
July 10, 2012 14:08
-
-
Save possan/3083478 to your computer and use it in GitHub Desktop.
Simple asynchronous mapper in javascript
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
Array.prototype.mapAsync = function(mapper, finalcallback) { | |
var waiting = 0; | |
var that = this; | |
var newlist = []; | |
this.forEach(function(item) { | |
// console.log('starting async mapper', item); | |
waiting ++; | |
setTimeout(function() { | |
mapper(item, function(newitem) { | |
newlist.push(newitem); | |
// console.log('async mapper done', item); | |
setTimeout(function() { | |
waiting --; | |
if (waiting === 0) { | |
// console.log('firing final callback'); | |
finalcallback(newlist); | |
} | |
}, 1); | |
}); | |
}, 1); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment