Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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

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 answer

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 answer

Source Link
Sean
  • 288
  • 1
  • 2
  • 9

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 answer