Skip to content

Instantly share code, notes, and snippets.

@pgte
Created January 9, 2011 23:08
Show Gist options
  • Save pgte/772128 to your computer and use it in GitHub Desktop.
Save pgte/772128 to your computer and use it in GitHub Desktop.
asynchronous iterative patterns in Node.js
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);
}
}
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();
}
});
}
}
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();
}
}
})();
}
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);
}
}
})();
}
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);
}
})();
}
function insertCollection(collection) {
for(var i = 0; i < collection.length; i++) {
db.insert(collection[i]);
}
}
function insertCollection(collection, callback) {
for(var i = 0; i < collection.length; i++) {
db.insert(collection[i], function(err) {
if (err) {
throw err;
}
});
}
callback();
}
@raroni
Copy link

raroni commented Feb 27, 2011

Your article (http://metaduck.com/post/2675027550/asynchronous-iteration-patterns-in-node-js) suggests that the callback is only called after the last insert in the also_wrong example.

I don't think this is correct. "i == (collection.length - 1)" would always be true, thus making it call the callback collection.length times. This illustrates it:

for(var i=0; 10>i; i++) {
  setTimeout(function() {
    console.info(i); // prints out 10, 10 times
  }, 500);
}

Let me know if I'm wrong :)

@pgte
Copy link
Author

pgte commented Feb 27, 2011

@RasmusRn Right you are, correctd, thanks!
Should be wrapping the i var inside a closure.
One more reason it's also wrong :)

@raroni
Copy link

raroni commented Feb 27, 2011

No problem!

Thanks for the article! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment