devNinja stuff
What is “callback hell” and how can it be avoided?
so callback hell is when a function’s parameters include another function that itself has parameters, one of which is yet another function that… HAS ANOTHER FUNCTION WHOSE PARAMETERS HAS A FUNCTION FOR AN ARGUMENT (and so on)! all these functions as parameters will have you going ARRRRRG (get it? arg? argument? i’ll show myself out…). this kind of code has a signature resembling a triangle, as such:
how can you avoid callback hell? well obviously you could just git gud (but no one wants to hear that), so i’ll give a few general ideas (because you could easily write a book, or even volumes, on this topic).
so you could at the very least use some comments. using comments could make small examples of callback hell hardly a hell at all. or, even more sensibly, you could chop your function up into smaller functions. you could use async await to construct functions that wait on other functions to complete before they run. you could use promises (which are similar to async await) which allow you to sort of chain functions that run asynchronously to the rest of your code (check out bluebird). there are actually many modules for node.js which amplify the utility of these methods or otherwise provide ways to avoid callback hell for common tasks (KOA 2, bluebird, Async.js, Pluralize, qs, clone). i may revisit this blog post and flesh out the utility of these, and generally beef up this answer. stay tuned!
What are “stubs” in Node.js?
quite simply, stubs are just fake data and fake endpoints you can attach to development-stage code to replace the actual code you may not yet have access to, or even to replace links which might not be up at the time of development. so you could use stub objects, links, arrays, etc. that allow developers to create functionality even when they don’t have the exact information that will be present in production-stage code.
What are “streams” in Node.JS?
streams in node.js are similar to streams in other applications, like when you watch YouTube videos or stream on Netflix. instead of downloading an entire video in order to watch it, you are given a pre-buffered amount of data that allows you to start watching immediately while it continues to load (buffer) the next chunk of data. additionally, as you finish watching chunks, that cached data can simply be deleted as you go. this way, you do not need more space than the amount it takes to watch comfortably without waiting for buffering (so like, 5 minutes ahead or something). so streams allow you to start watching immediately, and not require a lot of disk space to continue watching.
this is similar to how streams are utilized in node.js. there’s an API that greatly eases the ability to produce such code, https://nodejs.org/api/stream.html
What does “chaining” mean in Node.JS?
chaining is as simple as this: myString.toUpperCase().trim().etc
the trick is to make sure each function returns a value (using something like .push()
for example would just return the new length instead of the array, so be careful). you could get really fancy with these chains, even using them on your own functions, but you would have to get pretty clever using the this
keyword. for example, if you were to chain your own methods that did things like capitalize words (using the substring
method instead of higher order methods, for the sake of example), you would want to make sure you return this
at the end of those functions before chaining multiple utility functions together, so that the functions didn’t just return undefined
and leave you staring at your computer screen with your mouse in your hands. if this
doesn’t make sense, then you aren’t alone, but it’s time to get to researching if you want to use JS instead of JS using you.
Explain “console” in Node.JS.
from nodejs.org:
Anyone familiar with browser-side development has probably used
console.log
for debugging purposes - Node.js has implemented a built-inconsole
object to mimic much of this experience. Since we're working server-side, however, it wrapsstdout
,stdin
, andstderr
instead of the browser's debugging console.
i will revisit this question at a later date to give details on stdout
(standard output), stdin
(standard input), and stderr
(standard error).
Explain exit codes in Node.JS. List out some exit codes.
exit codes are a godsend for debugging as they give you a light at the end of the tunnel when you’re really in the weeds trying to figure out why something isn’t working. but they aren’t just useful for the developer, as the machine itself can take an error code as an argument to a tree of possible new functions/actions and avoid a crash.
What is the difference between cluster and non-cluster Index?
a clustered index is very simple, and by default in MySQL you already have one because you have a primary key. the way it works is like a phonebook, where your data itself is organized by the reference point (a-z). but a non-clustered index requires you to create it manually, selecting whatever reference point you want. an example of this would be an index in a textbook, which references page numbers and then groups the fields alphabetically. note that a textbook’s index is placed at the back of the textbook, in addition to its actual data (the chapters), and takes up additional space. so this is important to keep in mind when trying to speed up queries based on reference points (fields) other than a primary key. they can be very powerful time-savers, but they also take time to create, as like i said, your computer is actually creating a reference point for every piece of data in your database.
so clustered indexes organize your actual data, and you can only have one (imagine a phonebook sorted by number as well as by name… your data would have to repeat itself which RDBMS cannot do since primary keys require uniqueness). and non-clustered indexes provide additional indexes to speed up searches based on the field (or fields… see composite index) that you wish would yield quicker results (like searching users by name in addition to searching users by primary key, which only helps the developer).
What are user defined functions? What are all types of user defined functions?
what isn’t a user-defined function?