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....
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...
}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.
You can further simplify that chain() function by removing promises altogether:
function chain () {
(function loop (i) {
if (i > 5) {
// set state to failed
return;
}
check((err,result) => {
if(err){//set state to failed
return;}
if (!result) {
return loop(i + 1);
}
// set state to finished
});
}(0));
}
Callbacks are much simpler and less error prone:https://medium.com/@b.essiambre/continuation-passing-style-p...
CPS in scheme works because proper tail call optimization exists, so your call stack doesn't explode. In current JS, the only way to make that work is performing a nextTick or setTimeout on every single function which trashes performance of simple things due to exiting your code back to the event loop all the time.
<stands on soapbox>
There's a special place in hell reserved for companies that deviate from standards because of petty disagreements. Proper tail calls have been a part of the JS spec for 5 versions now. They are implemented in Safari and in popular embedded engines like duktape. MS and Firefox refused to implement. They were in v8 behind a flag, but were removed.
v8 devs pitched a fit about stack traces disappearing (even though they disappear across the event loop anyway). The webkit team showed that Chicken scheme's shadow stack works just fine and it's been in production for a long time now (without any issues I'd add). Instead, they insisted that they wanted to revise the standard to require marking tail-recursive functions.
They also talked about decreased performance, but JSC team seems to have implemented it with a minimal performance impact, so it's definitely possible. More importantly, JS is a language to make development easy -- we have wasm in development for when speed becomes an issue and it's faster anyway (the label proposal would have also solved the issue by giving devs a choice).
They then dropped their own proposal, but still simply refused to re-add the feature as per the spec in a bid to force a spec change. I have some understanding of not turning a feature on until a newer proposal is considered, but when they dropped that proposal and still refused to implement, they lost all credibility in my estimation.
https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-...
https://www.more-magic.net/posts/internals-gc.html
<soap-boxing intensifies>
I think this is indicative of a general "we know better than you" attitude on the part of the engine designers (especially the ones that sit on the spec committee). Another amazing example of this is the private variable proposal. NEVER has a JS proposal received such outspoken disapproval. Complaints range from the mostly insignificant "it's ugly" to the important "it goes against JS's core prototypical nature" to the very important "refactoring and maintaining this will be a nightmare due to dedicated syntax and JS being so dynamic", "it looks like a normal JS property that is hidden, but it in fact has almost nothing in common with normal properties even though the access syntax looks similar" or "this screws with inheritance, subclassing, destructuring syntax, etc".
Instead of listening, they locked down all dissenting discussions on the proposal's github and are going full steam ahead. EDIT: some discussions are unlocked (see below for a decent argument summary).
https://github.com/tc39/proposal-class-fields/issues/150
Decorators, pipe operators, tuples, immutable records, and slice notation are all well-understood ideas. Most don't require huge amounts of work to implement, but result in large quality-of-life improvements for developers. Most importantly, they aren't really controversial at all and most are easily transpilable so devs could start using them today (unlike private vars which either don't actually transpile correctly or create a mountain of unreadable garbage)
Why can't we work on these things that have such wide user impact instead of forcing through controversial changes that only benefit a very few people?
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... fetch((err, res) => {
if (err) {
// yay - error handling! :)
}
const myJson = res.json();
// do stuff
}
What's so unreadable about this? Even if it is 4 calls it would still be readable because if you have any sizeable amount of logic you should be refactoring it into several functions anyway with the above function gluing them together. Much easier to write tests for too! check()
.then(result => {
if (result) {
// set state to finished
}
check()
.then(result => {
if (result) {
// set state to finished
}
check()
.then(result => {
if (result) {
// set state to finished
}
check()
.then(result => {
if (result) {
// set state to finished
}
check()
.then(result => {
if (result) {
// set state to finished
}
// set state to not done
})
.catch(error => // set state to failed);
})
.catch(error => // set state to failed);
})
.catch(error => // set state to failed);
})
.catch(error => // set state to failed);
})
.catch(error => // set state to failed);
For the love of everything that’s sacred, please don’t do this. The strength of promises is that they can free us from that exact kind of callback hell, and they do that by being chainable. const resultOrNull = await Promise.resolve(null).
then(result => result || check()).
then(result => result || check()).
then(result => result || check()).
then(result => result || check()).
then(result => result || check()).
catch(error => null);
Calling .then/.catch inside of a .then/.catch is a huge red flag. Almost always, you want to return the promise and chain instead.But you having the “.” dot one line above made my spine tingle in all the wrong ways. Not that it matters.
Pasting
check()
.then()
in node REPL gives : > check()
undefined
> .then()
Invalid REPL keyword
Pasting check().
then()
Gives the expected result.More generally, Javascript rules about whether a newline constitutes the end of a statement or not are pretty confusing (at least for me), so I prefer using a form that makes it explicit when a statement continues on the next line.
I'll admit the only reason I've noticed the domain is my corporate IT seems to have all sites on weird TLDs blocked by default :/
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
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.
Surely 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
function check() {
return new Promise(async (resolve, reject) => {
for(let i = 0; i < 6; ++i) {
if(await doCheck()) {
resolve(true);
}
}
reject();
});
}
or... async function check() {
for(let i = 0; i < 6; ++i) {
const status = await doCheck();
if(status) {
return status;
}
}
throw new Exception('failed the 6 checks');
}
Though, the same thing could certainly be accomplished with pure promise chains. One could also use the setTimeout method in situations where elapsed time is more important than a fixed number of calls.