2

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.

Blank
  • 7,088
  • 12
  • 49
  • 69
  • Generally I'd use the Async module if you have a series of of asynchronous tasks. https://github.com/caolan/async If you post some samples of your code I can give you some examples. – loganfsmyth Oct 18 '11 at 03:02

1 Answers1

3

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:

Then you can make things simple and take advantage of several flow control libraries which deals with issues of asynchronous code in node.js:

yojimbo87
  • 65,684
  • 25
  • 123
  • 131
  • Also http://callbackhell.com/ is a really nice guide to asynchronous javascript. Trying to force node.js into a synchronous api is usually a bad idea. Also there is nothing wrong with using a named function only once. Even if only for code readability named functions are worth it – Noah Mar 25 '13 at 17:14
  • The second url changed. I guess this is what you meant: https://shinesolutions.com/2011/08/26/asynchronous-code-design-with-node-js/ – feka Mar 23 '17 at 15:05