co

Found out that examples in MongoDB use co generator to make it easier to write and read async functions:
https://github.com/tj/co

Something very complicated (because of multiple callbacks) like:
var db = new Database('test', new Server(connectionString.server, connectionString.port));
db.open(function(err, db) {
if (err)
cb(err);
var adminDb = db.admin();
adminDb.listDatabases(function(err, dbs) {
if (err)
cb(err);
cb(null, dbFound);
});
});

becomes very simple when using co:

return co(function*() {
try
{
let db = yield MongoClient.connect(url, {ssl:connectionString.ssl});
let dbs = yield db.command({listDatabases:1});
}
catch()
{}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s