Is await considered a bad coding practice? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm new to asynchronous programming and I would like to know if using await is considered a bad coding practice. I am asking this because it looks like it is possible to let the callback function do the waiting instead of letting the entire program suspend everythng until the rpocess complete.
Thank you and I appreciate your idea on this.

No, it’s not. It’s just an approach (very useful sometimes).
In modern systems complexity is so high, that in many cases having clear code is much better that having super effective code. Just an example: imagine the case when you need to make several async things one by one (maybe, fetch data based one previously fetched results). You can do it making a chain of several .then(). After that you’ll need to also add .catch(). And at this point you’ll find yourself writing spaghetti code which is a bit messy.
The other option is just make this async function with sequential calls and some logic between them. And this will look much better

Related

POST or GET React An idea? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
my question and quite simple, what is the best solution.
The context, I have an array that contains several objects.
The 1st solution and the following, is it better to POST only the elements that I need (ie not all the elements)
The 2nd solution, rather post the whole table and make a get to sort the elements that I need
do you have an idea what is the easiest to do?
Thank you Neff
Well your question may seem a little ambiguous. There are a few things to clarify, for example the usage of this POST method. However, here is my solution to my best understanding:
Assuming that the term "best" means the overall optimal option, I believe that you should always use the first solution since it benefits both the user and server. Sending minimal data packets reduces the need of bandwidth, in this case benefiting both the user and server. It also helps the server to get things done quicker, it is rather more beneficial for lightweight processing to be done in order for the server to free up CPU for other operations, i.e. handling more requests.
Though results may vary depending on individual need, this is my overall insight into the topic. Glad to answer your question.

Why forEach return void? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm using Typescript for a Node application and I love to fully use the power of Javascript with all the good stuff of Typescript. Line after line I took up the habit to chain functions, to use arrow functions, to user carrying and partial functions and so on.
What I really cannot understand is why the forEach function return void instead of something useful I can use to perform a chainable, just one-line and elegant piece of code.
My question is about the design considerations behind this choice that at first glance seems to be just annoying and the implications a return value can cause.
Because that's what forEach does. It's like a for loop; it isn't meant to evaluate to anything useful. It's intended to be used to carry out side effects using a function. If you need it for anything other than that purpose, it's the wrong tool for the job.
If you want a looping function that evaluates to something, use map or reduce instead. map will return a new list based on the old list, and reduce will return a reduced value from iterating over the list.

Asynchronous programming in JS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I often hear something like "working knowledge of asynchronous programming" regards JavaScript in job descriptions etc, but I'm not sure what it mean - is it about callbacks and promises or is there something else to it? I'd appreciate if someone could explain this to me.
Yes, effectively. More generally, it's about understanding the asynchronous nature of the most common JavaScript environments (web browsers, Node.js) and being fully versed in using callbacks, promises, async/await (in modern environments), etc. Understanding the closures-in-loops problem, why you can't return the result from an asynchronous call, that code that looks like it's below other code in a function may run earlier than the code apparently above it (because the code above it is in a callback), etc.

Which way is better to call a jquery function twice? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a javascript jquery function which I need to use twice, in 2 different files.
Now, the question is, which is the better way to implement it, performance-wise. Should I declare function global using 'window' variable, or should I declare the function in each of the files?
Please keep in mind that traffic is not an issue, the script will be used from the hard-drive.
Thanks for your help.
My answer follows the DRY Principle (Don't Repeat Yourself). If you put the function in both files, and later find a bug on Page #1, someone has to remember that the function is duplicated on Page #2 as well (in this case, assume the bug report says "Page #1 doesn't work properly"). From that bug report, would the developer know to also modify Page #2? To avoid the human error piece, I'd always recommend you don't copy/paste functions into multiple locations.
Performance wise, if you're not concerned about traffic, the difference is nanoseconds slower wrapping it in a shared function as you do have to create an additional stack frame for the shared function call that in turn calls jQuery, but we're really talking nanoseconds. For a few nanoseconds lost, I'd say DRY is the way to go.

javascript squared [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What is better to use in javascript to square some value.
Math.pow
Math.pow((circle2.x - circle1.x), 2)
Or my own function
square(circle2.x - circle1.x);
function square(a){
return a*a;
}
It is nearly always better to use a library then write your own code, unless you have a good reason not to. Reasons why:
It saves you work.
It is well-tested code, while your code might introduce bugs.
Someone else who looks at your code will know what Math.pow is, so it makes your code easier to read.
Obviously this is a very simple case, in which your own function is unlikely to cause a problem. Still, it is better to get in the habit of using libraries whenever possible.
By the way, Math.pow performs a lot of handling of special cases. This illustrates that even a simple function can have more pitfalls than it appears at first. By using a library, you don't have to worry about handling all of the edge cases yourself.
You should always prefer to use library functions - what is the point in reinventing the wheel?
Library functions are optimized and may cater for some corner cases you are not aware of...
Are you going to provide own implementation of multiplication as well?
For a power 2 specifically I would use multiplication because you really don't lose any legibility. Even more if speed is important, multiplying will probably be faster than function calls.
edit: I mean don't even create a function... just a*a.

Categories

Resources