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.
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?
https://stackoverflow.com/questions/36419713/are-es6-classes...
However, when you want a more fine-grained concurrent execution of different asynchronous things, then() still could be very useful.
const task2then3 = async () => task3(await task2());
const [result1, result3] = await Promise.all([task1(), task2then3()]) async function getResult3(){
const res = await task2()
return task3(res))
}
const [result1, result3] = await Promise.all([task1(),
getResult3()])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.
PS I should clarify await and Promise.all/any work fine:
await Promise.all(items) (async () => { set(await get()); })();
get().then(r => set(r));
Ignoring error handling I'll often choose the second option. 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. get()
.then(handleResult)
.catch(useFallbackResult)
.then(updateState)
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. const result = await get()
set(result)
But you do you.Current node and console have top level await BTW.
In other words, your complaints have disappeared in node, and will soon in browsers: https://github.com/tc39/proposal-top-level-await
So yes:
set(await get())Here's a simple question to ask yourself: what if non-IO operations allowed you to use the .then() syntax? If you have the choice to use:
1 + 1.then(function(result) { ... }
vs const result = 1 + 1
Which would you choose?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."