When do I use a callback function in JavaScript programming - node.js

I have a very simple app, that mainly collects data from the native ‘os’ nodejs module and then write to the DOM. I don’t really care which function updates the page first or last, and where my functions do not rely on one another, do I need to use callback functions?
The underlying noob question I have is, “when exactly do I need to use callback, vs when I don’t?”
Is there a millisecond time benchmark, where if a function is going to take longer than n seconds, then use a callback?

Related

Smart way to store a function during execution

I have a function that I modify frequently. I want to be able to store the exact version of the function used during execution and then use that to recreate the result. Is there a smart way to do that?

posting to same URL but using 2 different functions

a portion of my application involves creating tests (i.e., picking x-number of questions from a filtered set of questions). The user is able to determine how big they want the test but to do so I need to calculate on the server how many questions are available. The function which creates the test is sent through this post:
app.post('/user/create_test', users.create_test);
As the user changes filters, I would like to determine the number of questions available... All I can think of is to use AJAX post to send the filter information but it will be passed to the same function as creating a test would... is there any way to post to the same URL but determine which function you execute?
Consider creating another function. - The best way to do Restful API's.
Consider renaming to app.post('/user/test').
The second function could be app.post('/user/test/filters').
Or make a single POST request and make sure your function does both creating and filtering.
In general, the design of the app lacks maturity. Rethink the client-server communications.

Passing variables to all modules

I'm building a service which is fragmented across multiple modules that are required when necessary.
I need to access the "request" variable from the router in all my modules.
My current solution (which has been suggested in other threads for passing variables in general) is to pass it to each required module:
var a_module = require('./a_module')(req);
And exporting each module as functions:
module.exports = function(req) {
...
}
But it is verbose and involves having to export my modules as functions, and only having access to this variable in the scope of the exported functions. Ideally I would like to be able to access the variable across the entire required module.
Is there any other elegant way to do it that I am missing? Like declaring the req variable as a global across the entire application?
This question is going to solicit opinions not answers, so it's not a great fit for stack overflow, but here's my $0.02.
You need to step back and ask yourself if you have really written so many modules that need access to a request object. I don't think you have. What you should be writing are functions that take the specific data they need - no more, no less. All these functions almost certainly don't need the entire request. How many of them really need access to every HTTP header, for example? Think of your program as a set of operations on domain objects/data. For example, maybe there's a function that takes a user account record and promotes it from a regular user to an administrator. All that function needs is the user account. It should not be coupled to an HTTP request object.
Just write a bunch of cleanly decoupled functions that take a small number of precise parameters and do something useful with them. This is called "loose coupling". Then organize groups of related functions into a module. This is called "cohesion". Then use some "glue" code to extract the necessary parameters from the HTTP req object and pass them as arguments to these functions. These same functions should work for a command line interface or another non-HTTP interface. They will be easier to understand, test, and more long-lived if you code them that way instead of going nuts with every line of every module knowing about the current HTTP req object.

php's-`mysql_fetch_array()`-like actions in node.js?

I guess node.js' mysql drivers are async, but I'm not really sure what that means, so... The npm module for node.js allows for rows to "stream" or be "gathered up all at once".
Can someone show me where streaming and gathering are applied, and can someone show me how a mysql_fetch_array()-like (from php) operation would be done in node.js?
"Streaming" means that you get a callback for each row, with an argument that's an object corresponding to the row. The other option gives you a callback after all the rows have been fetched, with an argument that's an array of objects, each element corresponding to one row. Which one to use depends on how your application logic is structured.
The latter one sounds like what you're looking for, though (again depending on your application logic) it might or might not be the best way to do it.

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:
async
step

Resources