This does what I need it to using async.js:
db.open(function (err, client) {
async.waterfall([
function (callback) {
client.createCollection("docs", callback);
},
function (collection, callback) {
for (var i = 0; i < 100; i++) {
collection.insert({c:i}, function() {});
}
callback(null, 'ok');
}
],
function (err, result) {
console.log('result: ', result);
});
});
By putting the async code inside the anonymous function passed to db.open the 'client' variable was visible to the first async function. Got that idea from this SO answerthis SO answer