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
Related
I am quite new to event source concept but got glimpse that i need to divide my each task or operation to event and save those, but my basic do but is if for each request if you save each step as event then should not be the db size increase and large in short time also how to read it properly.
I just like to know if there any any blog or article or example that you guys could provide for reference and answer the above concerns.
I'm not sure if you are doing that for learning or for a real project, but in both cases I would suggest you to have a look at https://www.eventstore.com.
It is a db modeled to do event sourcing, also if not using it studying how it works will tell you what things you need to have in order to do it
My question may seem dumb to the experienced but hey, I am just trying to learn. In a react-redux-thunk setup or for that matter any similar setup, should I use complex joins at backend or return normalized values to the front end as much as possible and use something like redux selectors to perform something similar to joins.
The second approach it feels will let me keep the state light but at the same time without proper algorithms, things can get messy. Like running three nested loops increasing time complexity etc.
Any thoughts or pointers to articles on best practices in this regard?
Should I use complex joins at backend?
Yes. In case if you have complex logic/data structure and need more computational power to do calculation/mutation with the data.
Try avoiding too many computation at UI for better user experience.
Server side languages Java, C# etc... shine well for this use case.
(or)
Return normalized values to the front end as much as possible and use something like redux selectors to perform something similar to joins?
Yes. In case if you have plain data structures and you are not performing too many manipulations of any nested structure at UI.
Check this for the ways to normalise your redux store data.
As a normal user, I am ok to wait a fraction of second more for server response whereas any lag in using my application after loading is not better experience (Clicking/Navigate to other tabs etc...).
To answer your question in one word: It depends.
Remember only user experience matters at the end.
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?
As I understand, you are not supposed to use parallel processing to do stuff in Revit through its API. More information is available at
http://thebuildingcoder.typepad.com/blog/2014/11/the-revit-api-is-never-ever-thread-safe.html
However, I am currently dealing with a problem of getting (and only getting) and then examining a lot of data to make some decisions. A sample code using TPL is given below, that seems to be working:
Parallel.ForEach<Element>(allDocumentElementsNotVisibleInCurrentView,
parallelOptions,
element =>
{
MyDerivedElement iaElement = new MyDerivedElement(uiDocument.Document, element, ElementStatusInView.Found);
// The following condition line does the real heavy lifting and can be a lengthy process, involving data extraction from Revit, such as bounding boxes, geometry and curves
if (iaElement.IsContained(iaView))
lock (result)
result.Add(iaElement);
});
So my question is, given that the above seems to work in tests, is it a good idea to let this one pass on parallel processing?
Thank you for your help!
The Revit API cannot be used outside a valid Revit API context, and such a context is only provided by a Revit event notification to be processed within a Revit event handler. The most common event is the external command Execute method. In the past, this requirement was not strictly enforced. However, use of the Revit API outside a valid Revit API context can lead to a crash and data corruption. Read-only access may still work, but is risky. I certainly would not store the Element instance itself. Storing the ElementId seems like a safer bet. Please expect to crash at any time.
My recommendation would be to reduce the Revit API interaction and processing to an absolute minimimum, collect all the data required for processing, terminate the Revit API interaction after collecting the data, and then run the pure calculations with no further Revit API interaction in a separate thread or several threads at a later point after leaving the Revit API context.
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.