我在一个项目中使用 Express,并且一直在使用 Cradle 尝试 CouchDB 数据库。虽然出于性能原因,异步执行的想法很酷,但它使我的代码对于需要连续进行多个数据库调用的例程来说非常混乱。
是否可以在不使用回调的情况下进行摇篮呼叫?或者,我认为更正确的是,是否有一种更好的方法来组织代码,而不涉及为了获取数据库查询结果而相互嵌套 3 或 4 个匿名函数?该代码仅在一个地方使用,因此对我来说使用仅被调用一次的命名函数没有意义。
I'm using Express for a project, and I've been trying out the CouchDB database using Cradle. While the idea of asynchronous execution is cool for performance reasons, it's making my code really a mess for routines where I need to make several database calls in a row.
Is it possible to make cradle calls without using a callback? Or, I suppose more correctly, is there a better way to organize the code that doesn't involve nesting 3 or 4 anonymous functions within one another just to get at database query results? The code is only used in one place, so it doesn't make sense to me to use named functions that will only be called once.
发布评论
评论(1)
据我所知,cradle 只有异步 API。
我首先建议阅读以下有关流量控制主题的文章,以便更全面地了解正在发生的情况:
,您可以使事情变得简单,并利用几个处理 Node.js 中异步代码问题的流程控制库:
As far as I know cradle has only asynchronous API.
I would first recommend to read following articles on flow control topic in order to get a bigger picture of what's going on:
Then you can make things simple and take advantage of several flow control libraries which deals with issues of asynchronous code in node.js: