Node.JS中的ASYNC

我对Node.JS和callback中的asynchronous是新鲜的。 你能不能让我知道这是否是一个asynchronous调用的正确方法?

function myFunc(schema) { async.each(Object.keys(schema), function(variable) { for (item in schema[variable]) { for (field in schema[variable][item]) { // Some operations go here } createDB(item, schema[variable][item]); } }); } function CreateDB(name, new_items) { ddb.createTable(selected_field.name, {hash: ['id', ddb.schemaTypes().number], range: ['time', ddb.schemaTypes().string], AttributeDefinitions: new_items }, {read: 1, write: 1}, function(err, details) { console.log("The DB is now created!"); }); } 

谢谢

这将是一个办法,所有艰难的我不是callback的粉丝,我更愿意使用承诺 。
这种方法只会传播所有的错误到cb1 ,如果你想处理错误之间的任何地方你应该包装或尝试修复的东西 。

如果你要在内部循环中进行asynchronous操作,那么你还需要一些额外的asynchronous重构的乐趣。

 function myFunc(schema, cb1) { async.each(Object.keys(schema), function(variable, cb2) { async.each(Object.keys(schema[variable]), function(item, cb3) { for (var field in schema[variable][item]) { // Some operations go here } createDB(item, schema[variable][item], cb3); }, cb2); }, cb1); } function CreateDB(name, new_items, cb) { ddb.createTable(selected_field.name, {hash: ['id', ddb.schemaTypes().number], range: ['time', ddb.schemaTypes().string], AttributeDefinitions: new_items }, {read: 1, write: 1}, cb); }