Created
January 9, 2011 23:08
-
-
Save pgte/772128 to your computer and use it in GitHub Desktop.
asynchronous iterative patterns in Node.js
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 insertCollection(collection, callback) { | |
for(var i = 0; i < collection.length; i++) { | |
(function(i) { | |
db.insert(collection[i], function(err) { | |
if (err) { | |
callback(err); | |
return; | |
} | |
if (i == (collection.length - 1)) { | |
callback(); | |
} | |
}); | |
})(i); | |
} | |
} |
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 insertCollection(collection, callback) { | |
var inserted = 0; | |
for(var i = 0; i < collection.length; i++) { | |
db.insert(collection[i], function(err) { | |
if (err) { | |
callback(err); | |
return; | |
} | |
if (++inserted == collection.length) { | |
callback(); | |
} | |
}); | |
} | |
} |
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 insertCollection(collection, callback) { | |
var coll = collection.slice(0); // clone collection | |
(function insertOne() { | |
var record = coll.splice(0, 1)[0]; // get the first record of coll and reduce coll by one | |
db.insert(record, function(err) { | |
if (err) { callback(err); return } | |
if (coll.length == 0) { | |
callback(); | |
} else { | |
insertOne(); | |
} | |
} | |
})(); | |
} |
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 insertCollection(collection, callback) { | |
var coll = collection.slice(0); // clone collection | |
(function insertOne() { | |
var record = coll.splice(0, 1)[0]; // get the first record of coll and reduce coll by one | |
db.insert(record, function(err) { | |
if (err) { callback(err); return } | |
if (coll.length == 0) { | |
callback(); | |
} else { | |
setTimeout(insertOne, 0); | |
} | |
} | |
})(); | |
} |
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 insertCollection(collection, callback) { | |
var coll = collection.slice(0); // clone collection | |
(function insertOne() { | |
var record = coll.splice(0, 1)[0]; // get the first record of coll and reduce coll by one | |
try { | |
db.insert(record, function(err) { | |
if (err) { callback(err); return } | |
if (coll.length == 0) { | |
callback(); | |
} else { | |
insertOne(); | |
} | |
} | |
} catch (exception) { | |
callback(exception); | |
} | |
})(); | |
} |
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 insertCollection(collection) { | |
for(var i = 0; i < collection.length; i++) { | |
db.insert(collection[i]); | |
} | |
} |
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 insertCollection(collection, callback) { | |
for(var i = 0; i < collection.length; i++) { | |
db.insert(collection[i], function(err) { | |
if (err) { | |
throw err; | |
} | |
}); | |
} | |
callback(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No problem!
Thanks for the article! :)