Is it possible to make cradle calls synchronously?
문제
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.
해결책
Is it possible to make cradle calls without using a callback?
As far as I know cradle has only asynchronous API.
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?
I would first recommend to read following articles on flow control topic in order to get a bigger picture of what's going on:
- Understanding Event-driven Programming
- Asynchronous Code Design with Node.js
- Understanding event loops and writing great code for Node.js
- Node.js: Asynchronous I/O for Fun and Profit
- Control Flow in Node
- Control Flow in Node Part II
- Control Flow in Node Part III
- Asynchronous code in node.js
Then you can make things simple and take advantage of several flow control libraries which deals with issues of asynchronous code in node.js: