A brief look at async-await
javascript.christmas
A brief look at async-await
1–10 of 47 posts
Re: A brief look at async-await
#2I tried to look up a github reference on the page, so I could send a PR to fix the linked article, but it is not available.
Re: A brief look at async-await
#3There should be some review process for these .christmas articles. I get it - creating tons of articles quickly is hard, but the core message should not be incorrect. I tried to look up a github reference on the page, so I could send a PR to fix the linked article, but it is not available.
If you could share what you found incorrect, we are happy to update the article accordingly
Re: A brief look at async-await
#4There should be some review process for these .christmas articles. I get it - creating tons of articles quickly is hard, but the core message should not be incorrect. I tried to look up a github reference on the page, so I could send a PR to fix the linked article, but it is not available.
We decided to keep the content in a private repo until every article is posted. If you could share what you found incorrect, we are happy to update the article accordingly
This (incorrect) message is then repeated again ("and act synchronously").
There are some other - minor - issues with rest of the article. I.e. asyncFunction and someFunction are not equivalent and as such can cause confusion (the same issue is in waitAndCheck vs chain.
Re: A brief look at async-await
#5 function chain () {
(function loop (i) {
if (i > 5) {
// set state to failed
return;
}
check().then((result) => {
if (!result) {
return loop(i + 1);
}
// set state to finished
}).catch((error) => {
// set state to failed
});
}(0));
}
This is one of my pet peeve with proponents of promises actually. They come up with convoluted examples to argue against callbacks but it's not the callbacks that are a problem, but the person writing the code.Actually, I would argue that promises really made no sense until "await" became available because they introduce extra complexity and (small) performance penalty for no added benefit. Worse, it encourages less experienced developers to write code in a serial manner in situations where this is not necessary (a much bigger performance problem).
Even with "await", the only time when it is really useful is when there have to be a several asynchronous operations that must happen serially because they use the result of the previous call. It depends on your field of work, of course, but in my experience these situations are really not that common.
Some time ago I wrote an article comparing performance of callbacks vs async/await but it is also an example to show that callback code does not have to be more tedious to write:
https://gir.me.uk/posts/node-8-async-await-performance-test....
Re: A brief look at async-await
#6I appreciate that the author is trying to illustrate something with an intentionally contrived example, but there is really no need for "await" in his "chain()" function: function chain () { (function loop (i) { if (i > 5) { // set state to failed return; } check().then((result) => { if (!result) { return loop(i + 1); } // set state to finished }).catch((error) => { // set state to failed }); }(0)); } This is one of…
While I agree the author’s examples are convoluted, the async-await code is much easier to read, write, and validate. Why bother tracking state and having to keep tracking of JS lexical scoping when you can write "boring" code that will be just as fast for the tiny iteration counts you'll be dealing with?
> Even with "await", the only time when it is really useful is when there have to be a several asynchronous operations that must happen serially because they use the result of the previous call. It depends on your field of work, but in my experience these situations are really no that common.
Concurrent tasks are more pleasant with await as you can combine it with Promise.all(...) to get efficiency and legibility:
async function doStuff() {
// Fetch things concurrently
const [
foos,
bars,
bazs,
] = await Promise.all([
getFoos(),
getBars(),
getBazs(),
]);
// Do stuff with them...
}Re: A brief look at async-await
#7Surely a better comparison for the async function would be a chain() that manually recreates the loop using a parameter or local captured variable, like shown below. They're still complex but if anything show the complexity more clearly, especially if 6 is not hardcoded but could be passed as a parameter. I suppose having the unrolled version from the article plus one of these would be best of all, to show the problem from all angles.
function chainWithParam() {
var checkAndIterate;
checkAndIterate = (result, i) => {
if (result) {
// set state to finished
return;
}
if (i == 6) {
// set state to not done
return;
}
check()
.then(result => checkAndIterate(result, i + 1))
.catch(error => /* set state to failed */);
}
checkAndIterate(false, 0);
}
function chainWithCapturedLocal() {
i = 0;
var checkAndIterate;
checkAndIterate = result => {
if (result) {
// set state to finished
return;
}
if (i == 6) {
// set state to not done
return;
}
++i;
check()
.then(checkAndIterate)
.catch(error => /* set state to failed */);
}
checkAndIterate(false);
}
I'm not a JavaScript programmer so there could be mistakes in the above code. I'd be interested to know if there is, especially whether it was really necessary to declare the checkAndIterate variable on a separate line to its assignment.Surely the "manual" function could be implemented with some sort of loop-like
Re: A brief look at async-await
#8Earlier quoted context omitted.
We decided to keep the content in a private repo until every article is posted. If you could share what you found incorrect, we are happy to update the article accordingly
> "thus forcing the code to be synchronous" This (incorrect) message is then repeated again ("and act synchronously"). There are some other - minor - issues with rest of the article. I.e. asyncFunction and someFunction are not equivalent and as such can cause confusion (the same issue is in waitAndCheck vs chain.
Additionally, we included a link to the "Top level await" proposal. Currently stage 3.
Re: A brief look at async-await
#9I appreciate that the author is trying to illustrate something with an intentionally contrived example, but there is really no need for "await" in his "chain()" function: function chain () { (function loop (i) { if (i > 5) { // set state to failed return; } check().then((result) => { if (!result) { return loop(i + 1); } // set state to finished }).catch((error) => { // set state to failed }); }(0)); } This is one of…
> This is one of my pet peeve with proponents of promises actually. They come up with convoluted examples to argue against callbacks but it's not the callbacks that are a problem, but the person writing the code. While I agree the author’s examples are convoluted, the async-await code is much easier to read, write, and validate. Why bother tracking state and having to keep tracking of JS lexical scoping when you can…
With the exception of necessarily serial code that I referred to, this statement is not true in my experience. Your "legible" example is only legible because you are not doing anything actually important like error handling and logging. In the real world each call may need different logging or even error handling logic and that is when promises really turn into a rats nest.
Validation, i.e. tests, are really the same regardless which approach you are using.
I do agree that async/await made promises actually usable, as I said above.
For running a bunch of asynchronous functions that do the same thing the semaphore + collector pattern works just fine and is not much more verbose.
Re: A brief look at async-await
#10I appreciate that the author is trying to illustrate something with an intentionally contrived example, but there is really no need for "await" in his "chain()" function: function chain () { (function loop (i) { if (i > 5) { // set state to failed return; } check().then((result) => { if (!result) { return loop(i + 1); } // set state to finished }).catch((error) => { // set state to failed }); }(0)); } This is one of…
Very common in my field. A single function that loads a file, parses metadata, then transforms the binary data into a different format can consist of 2-4 async calls that need to be executed in order. Await is incredibly helpful here, and makes code much more readable compared to the chain of then statements we previously used.
Even something simple as the fetch API to load a json file consists of two async calls in a row:
const response = await fetch('http://example.com/movies.json');
const myJson = await response.json();
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/U...