10
votes
Accepted
How does JS Promises works being single threaded
Parallel or out of order or asynchronous execution is not really tied to number of threads. Think of it. A single thread can still do the switching and do n things at same time, in bits and pieces. ...
7
votes
How does JS Promises works being single threaded
Promises in JS are a way to do asynchronous programming, which is not the same as multithreading. Essentially, in synchronous single-threaded code, when there's some some sort of IO, the processor ...
6
votes
Accepted
How do JavaScript engines convert async/await to promises under the hood?
This async/await code:
async function myFunc(doAwait) {
doSomething();
if (doAwait) {
await doSomethingAsync();
}
return doSomethingElse();
}
Would be basically equivalent to ...
3
votes
Which is more readable: early returns, or conditionals?
Disclaimer: this is opinion-based, based on my feelings about readability.
I generally prefer the early-returns style for things like aborting a process because of some reject reason, or for early ...
2
votes
Accepted
What is the advantage of flattening dependent Promises
The simplest option to have access to both customer and processingResult without nesting would probably be to use async/await (ES7 feature) like this (this would need to be inside an async function ...
2
votes
If callback function, promises and async/await patterns all can be used to achieve asynchronous behaviour then why don't we stick to one?
You seem to have reached the world of callbacks and async code recently. The reason there are multiple approaches is, like most things in programming, history.
The main asynchronicity in JS was in ...
2
votes
If callback function, promises and async/await patterns all can be used to achieve asynchronous behaviour then why don't we stick to one?
The premise of your question is that having a single solution is the obvious choice. So why do we have:
More than one programming language?
More than one operating system?
More than one sort of car?
...
2
votes
Writing elegant promises in Node.js
I think you're rejecting (ha!) the idea of async/await a bit too quickly; it's exactly what you need for the problem you're having, in terms of simplifying things.
You also can create a custom ...
2
votes
Creating a promise based API, from a message based API
I am afraid with that setup, you are out of options. Create a singleton instance that has a method that blocks until the reply is received. Anything else will result in weird, hard to track failures.
...
1
vote
Organizing Parallel Arrays of Promises / Async tasks
JavaScript does not support parallelism. What you can do is do something while waiting for something else, concurrently. In your scenario, you might process the contents of one file while waiting for ...
1
vote
How does JS Promises works being single threaded
The asynchronous task may be forked by the JS runtime, for example when only external systems are involved, but it may also get started/executed when there is no more other code to run. In the first ...
1
vote
Writing elegant promises in Node.js
You can throw instead of returning Promise.reject
You can replace the callback functions with arrows to reduce noise and avoid the self=this trickery
You could also create some helper functions for ...
1
vote
Which is more readable: early returns, or conditionals?
The important thing is to maintain a clear structure.
A simple switch/case statement (or a series of non-nested if statements) that contains a return within each and every case, will be perfectly ...
1
vote
Which is more readable: early returns, or conditionals?
This is quite opinion-based, but having only one exit point has its advantages.
The main reason to eschew the multiple conditionals is to avoid a ziqqurat of nested if's, which may also be difficult ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
promises × 26javascript × 21
async × 6
node.js × 5
es6 × 3
language-agnostic × 2
error-handling × 2
asynchronous-programming × 2
typescript × 2
design × 1
web-development × 1
api-design × 1
coding-style × 1
functional-programming × 1
xml × 1
anti-patterns × 1
event-programming × 1
conditions × 1
messaging × 1
angular2 × 1
asynchronous × 1
callbacks × 1
control-flow × 1
continuation × 1
language-discussion × 1