Asynchronous programming in JS [closed] - javascript

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.

Related

Is await considered a bad coding practice? [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 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

Why we use async/await in nodejs if it is already an asynchronous and in javascript also vice vera? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Sorry I am a noob and a very beginner with Javascript and NodeJS. :(
Pardon me for asking this silly question.
So I am here a little bit confused. I searched a lot but didn't get the appropriate understanding. My question is simple:
Javascript is synchronous and we use async/await for the callbacks.
Nodejs is asynchronous but also then we use async/await nodejs also.
I really don't get this. How's is it happening? Maybe someone can give me a clear picture of this with some proper and easy to understand examples.
Thanks!
Javascript and Nodejs, code written in both works synchronously. Based on functional requirement we make the execution of code asynchronous by adding async/await.

When to use Callbacks and Promises? [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 5 years ago.
Improve this question
I'm new to NodeJs and to JS actually. I coded my first application using callbacks everywhere and I reached a state where I couldn't handle the number of nested callbacks anymore (callback hell). So I searched for solutions and I read that Promises are one of the best ways to solve this problem. Btw, here's a good explanation for those who are interested: https://blog.risingstack.com/node-js-async-best-practices-avoiding-callback-hell-node-js-at-scale/
Therefore, I made a refactoring in the most critical functions using Promises and it works very well. However, I don't know if I should refactor all the application. How do JS developers do nowadays? Do they use Promises everywhere or do they keep using callbacks the most part of the time and use Promises only when there are too many nested callbacks? Is there a best practice for that?

NodeJS when to use promises? [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 6 years ago.
Improve this question
This might be little opinion based, but an important question.
Should you use promises only for async operations? If you have an api, then very likely, the full stack of functions in there should use promises. So then if I make some business logic functions, they also should return a promise. I wonder if this is good, that you make everything return a promise. I mean, I'm calling functions inside a while loop, where each of them returns a promise (the functions are chained). Is it slower to use promises inside a function? Is it good idea in a stack to combine promises and regular return values? I really need to learn this, so please don't close
If your function is (sometimes) asynchronous, it needs to return a promise.
If your function never does anything asynchronous, there's no good reason for it to return a promise, and you should avoid it. Keep it simple and synchronous.

Javascript development: When do you prefer sync over async? [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 7 years ago.
Improve this question
I was reading an MDN article, and it states that almost always async is a better approach than making synchronous calls from the browser to your back end. It then goes on to say "In rare cases, the use of a synchronous method is preferable to an asynchronous one." I was wondering what are some use-cases when we would prefer making synchronous requests ?
Synchronous AJAX requests completely block the UI (the same way that, for example, a native alert box does). The JavaScript concurrency model is based on leveraging asynchronous operations. I can't think of a single use-case where a synchronous request would have benefits over an async one.
As far as stylistic concerns, like "waterfalls" of callbacks, etc., these can all be managed fairly easily through different means. For example, various Promise libraries, or native ES6 features like "coroutines" (generators that deal with Promises) or even just native ES6 promises.
I think if you're running into a situation where you feel the need to call a synchronous request, then you need to step back and re-think the architecture of your application (not talking about you specifically, but just in general).
In Javascript the rule is async first! Only in case that is not possible go for synchronous !!

Categories

Resources