- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 async库,但我不知道如何在现实世界的例子中重写回调 hell 。我对串行方法以及与某些现有驱动程序的通信特别感兴趣。有人可以使用 async.js 系列方法重写以下源代码吗?它取自 this link .
我不固定使用 MongoDb。如果有人将其他一些回调 hell 示例重写为异步系列,那将是很好的展示。
除了异步库之外,我还会对任何替代解决方案感兴趣。但同样 - 在该解决方案中重写此示例(或显示其他完整示例),以便我们可以看到真实代码并进行比较。
var MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log("Connecting to " + host + ":" + port);
MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
db.dropDatabase(function(err, result) {
var collection = db.collection('test');
// Erase all records from the collection, if any
collection.remove({}, function(err, result) {
// Insert 3 records
for(var i = 0; i < 3; i++) {
collection.insert({'a':i}, {w:0});
}
collection.count(function(err, count) {
console.log("There are " + count + " records in the test collection. Here they are:");
collection.find().each(function(err, item) {
if(item != null) {
console.dir(item);
console.log("created at " + new Date(item._id.generationTime) + "\n")
}
// Null signifies end of iterator
if(item == null) {
// Destory the collection
collection.drop(function(err, collection) {
db.close();
});
}
});
});
});
});
});
最佳答案
像这样的 async.series(代码未经测试,仅供引用):
var async = require('async')
, MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
var collection, db
console.log("Connecting to " + host + ":" + port);
async.series([
// Connect to DB
function(callback) {
var connectionString = format("mongodb://%s:%s/node-mongo-examples?w=1", host, port)
MongoClient.connect(connectionString, function(err, ref) {
if (ref) db = ref
callback(err, ref)
})
},
// Drop DB
function(callback) {
db.dropDatabase(callback)
},
// Erase all records from the collection, if any
function(callback) {
collection = db.collection('test');
collection.remove({}, callback)
},
// Insert 3 records
function(callback) {
async.each(new Array(3), function(cb) {
collection.insert({'a':i}, {w:0}, cb);
}, callback)
},
//Check records count
function(callback) {
collection.count(function(err, count) {
if (err) return callback(err)
console.log("There are " + count + " records in the test collection. Here they are:");
callback(err, count)
})
},
//Indicate items
function(callback) {
collection.find({}, function(err, items) {
items.forEach(function(item) {
if(item == null) return
console.dir(item);
console.log("created at " + new Date(item._id.generationTime) + "\n")
})
callback(err, items)
})
},
function(callback) {
collection.drop(callback)
}
], function(error, results) {
// disconnect from DB
error && console.error(error)
db && db.close();
})
或者像这样(混合 waterfall 和系列方法,没有全局变量):
var async = require('async')
, MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log("Connecting to " + host + ":" + port);
async.waterfall({
// Connect to DB
db: function(callback) {
var connectionString = format("mongodb://%s:%s/node-mongo-examples?w=1", host, port)
MongoClient.connect(connectionString, callback)
},
// Drop DB
collection: function(db, callback) {
db.dropDatabase(function(err, result) {
var collection = db.collection('test');
callback(err, collection)
})
},
collModifications: function(collection, callback) {
async.series([
// Erase all records from the collection, if any
function(callback) {
collection.remove({}, callback)
},
// Insert 3 records
function(callback) {
async.each(new Array(3), function(cb) {
collection.insert({'a':i}, {w:0}, cb);
}, callback)
},
//Check records count
function(callback) {
collection.count(function(err, count) {
if (err) return callback(err)
console.log("There are " + count + " records in the test collection. Here they are:");
callback(err, count)
})
},
//Indicate items
function(callback) {
collection.find({}, function(err, items) {
items.forEach(function(item) {
if(item == null) return
console.dir(item);
console.log("created at " + new Date(item._id.generationTime) + "\n")
})
callback(err, items)
})
},
function(callback) {
collection.drop(callback)
}
], callback)
}
}, function(error, results) {
// disconnect from DB
error && console.error(error)
results && results.db && results.db.close();
})
关于javascript - caolan的异步库系列方法的真实世界示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19287660/
我想知道是否可以进行一系列计算并使用 async.each 方法获取返回值。一个非常简单的例子是计算数组中所有元素的总和。 function Calculator() { } Calculator.p
早些时候,我是一名 javascript 程序员,我使用来自 caolan 的库 async 来防止回调 hell async library . 在这些天里,我转向了 dart,异步风格与 java
我在 node.js 中使用 caolan 表单。我的架构如下所示: var News = new Schema({ name : String , index: Number }); 我的表单有
使用 Balderdashy/Waterline和 Caolan/Async ,我正在尝试并行处理多个 Waterline 查询。到目前为止,我发现的较短的是: const tasks = {
我是一名优秀的程序员,十分优秀!