Created
July 20, 2011 04:11
-
-
Save ox/1094317 to your computer and use it in GitHub Desktop.
JS ASync for MarkBao
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 = ['test', 'of', 'file']; | |
array2 = ['another', 'array']; | |
// just calls the callback and passes in this item | |
String.prototype.toArray = function(callback) { | |
callback(null, this); | |
}; | |
// test | |
array.forEach(function(item) { | |
document_ids = new Array(); | |
count = 0; | |
console.log(item + ': '); | |
array2.forEach(function(item2) { | |
// look it up | |
item2.toArray(function(err, documents) { | |
// because of async, | |
// the code moves on and calls this back later | |
console.log('got id'); | |
document_ids.push(documents); | |
count += 1; // one more process has finished | |
}); | |
}); | |
// once all of the processes have finished, we can run this. | |
if(count == array2.length) { | |
// use document_ids | |
console.log(document_ids); // shows [] | |
console.log('done'); | |
} | |
}); | |
// shows: | |
// test: | |
// got id | |
// got id | |
// [ { '0': 'a', | |
// '1': 'n', | |
// '2': 'o', | |
// '3': 't', | |
// '4': 'h', | |
// '5': 'e', | |
// '6': 'r' }, | |
// { '0': 'a', '1': 'r', '2': 'r', '3': 'a', '4': 'y' } ] | |
// done | |
// of: | |
// got id | |
// got id | |
// [ { '0': 'a', | |
// '1': 'n', | |
// '2': 'o', | |
// '3': 't', | |
// '4': 'h', | |
// '5': 'e', | |
// '6': 'r' }, | |
// { '0': 'a', '1': 'r', '2': 'r', '3': 'a', '4': 'y' } ] | |
// done | |
// file: | |
// got id | |
// got id | |
// [ { '0': 'a', | |
// '1': 'n', | |
// '2': 'o', | |
// '3': 't', | |
// '4': 'h', | |
// '5': 'e', | |
// '6': 'r' }, | |
// { '0': 'a', '1': 'r', '2': 'r', '3': 'a', '4': 'y' } ] | |
// done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment