> The default way promises are used is now async/await. `.then()` should not be introduced as anything more than 'you may find this in older codebases'.
No, because you might have to write explicit promises in the browser with async API that are callback based.
You need to understand how promises work to use async/await at first place anyway.
Just like it's better to understand how prototypal inheritance works in JS BEFORE even using a single class declaration.
"Just like it's better to understand how prototypal inheritance works in JS BEFORE even using a single class declaration. "
About that, does JS class inheritance now work like classes in java for example? Or is it still just syntactic sugar for the prototype inheritance? By your wording, I assume the later?
Yeah, like many people, I ended up making my own inheritance routines. Then when class came out, I was half rejoicing, half regretting to put up the work. But well, no thanks. I rather keep using my own far from perfect way of things.
Maybe some codebases mandate it, but it's very common to use either style when it makes the most sense. The classic syntax is by no means deprecated or superseded by async/await.
async/await is excellent syntax when you want things to happen one after another. That's the usual case, and that's what I find myself using 95% of the time.
However, when you want a more fine-grained concurrent execution of different asynchronous things, then() still could be very useful.
Often times operations are order-independent. Specifying those as sequential `await`-s does not make a lot of sense in those situations. I guess less fine grained control is desirable under such circumstances :-)
There's also other useful utilities such as Promise.all(), Promise.any() and Promise.race().
One problem. I’m increasingly coming across devs who’ve never used Promises directly, only async/await. They’re introducing all kinds of problems into code because they don’t have that foundational understanding of Promises. I think it’s essential to know how Promises work, even if we generally recommend async/await.
Also, Promise interface can do things that async/await cannot, such as Promise.all, and other unusual async execution scenarios. It’s important to have those tools in your belt.
Much cleaner. You can go even further by using point-free style:
get().then(set)
It's the try/catch blocks that make async/await truly worse than native promises. I use them sparingly because so often the .then syntax is both clearer and terser.
try/catch introduces new block scopes and interrupt control flow. I find this example very readable, but expanding it into try/catches would make it rather long winded and harder to see the pattern at work as you extend the sequence:
I prefer concision and functional expressiveness because it reduces the surface area for writing buggy code. Much like how using map instead writing a for loop can eliminate out of range index bugs, chaining promises brings useful guarantees about how the code I do write can be interpreted.
Yes, your example isn't complaining about then() being clearer than await (because it isn't) but rather complaining that top level await isn't everywhere yet.
Using then() doesn't pause execution of the callsite function, while await does. Surely that's a relevant difference here?
It doesn't seem unreasonable to use then() to express: "Run this anonymous function whenever the async thing completes. Meanwhile let's do this other stuff right now."