Earlier quoted context omitted.
You really end up creating promises manually, the vast majority are downstream from an IO call like fetch() or a database query.
This is absolutely correct why is it being downvoted lol
I Avoid Async/Await
151–160 of 242 posts
Re: I Avoid Async/Await
#152Earlier quoted context omitted.
This, very much this. async function() {} is much nicer than function() { return new Promise( (resolve) => resolve() ) }
I agree but second one could just be `() => Promise.resolve()`
Re: I Avoid Async/Await
#153Earlier quoted context omitted.
Many devs unnecessarily nest Promises like that. async function getData() { return new Promise((resolve) => { const res = fetch(…); resolve(res); }); } The response above is wrapped in THREE different Promises! One from fetch, one manually created, and one implicitly created by `async`. The code above behaves exactly the same as function getData() { return fetch(…); } or even just fetch(…);
> function getData() { return fetch(…); } I feel like there are advantages to making it `async function`, even if it's superfluous, because it signals to readers and to static code analysis that the function returns a promise. That's assuming the return type of fetch(...) can't be inferred by static analysis and developer tooling.
That is preposterous.
Re: I Avoid Async/Await
#154- - -
I’m in the process of gradually moving several decade-old JS projects from explicit Promise APIs to async/await. The reason for this is not the syntactic sugar (although that’s quite a benefit, and I disagree wholeheartedly with the article on that point). The reason is that, at least with native Promises, the actual runtime behavior of async/await is considerably better. Because await suspends the stack, where .then/.catch enter a new stack frame.
I do see this has been discussed some in another thread, but I feel like it deserves more direct attention. It’s true that you can use libraries like Bluebird or whatever to get more accurate stack traces. But this is done by tracking Promise chains in user space. This is fine but it comes with a cost. Even if the library is quite fast (as Bluebird is), you still incur the cost of downloading and parsing it. Especially on HN where JS bloat is a daily topic, I’d hope this can be appreciated.
In my case, this cost wouldn’t be worth it even if the Promise API were clearly better. These projects need to run efficiently on low power mobile devices with limited connectivity. And we need to be able to diagnose runtime errors to address outstanding bugs. A library is a non-starter, and native Promise APIs actively hinder progress. The behavior of async/await is clearly superior.
Suspending the stack, either with async/await or generators, affords significantly better debugging capabilities. It allows Error stacks and console.trace to take better advantage of source maps.
It‘s also a much better optimization target for browsers. This isn’t just academic: even if async/await is semantically equivalent to Promise, it’s not a guarantee that the underlying .then/.catch will even be called if there’s no explicit Promise in the code. I’ve seen cases where they’re not, which was baffling until I understood (and this is how I came to understand) the difference in runtime behavior.
Re: I Avoid Async/Await
#155Earlier quoted context omitted.
here's a real world example I use all the time: const wait = (ms = 500) => new Promise(resolve => setTimeout(resolve, ms)); then: async function () { await wait(1000); } Perhaps it's hard to grok but it sure is a handy one-liner. Of course in newer versions of node we can now do: import { setTimeout, } from 'timers/promises'; await setTimeout(100);
There's no merit of what should be a project wide function being a single line instead of 3 to avoid "hard to grok" syntax.
Re: I Avoid Async/Await
#156Re: I Avoid Async/Await
#157Re: I Avoid Async/Await
#158First off, async/await is promises. It's merely syntactic sugar. The point of async/await is not to never have the word "Promise" appear in your code. It's also not meant to be universally better than using Promises bare-bones. A lot of the argument appears to be the author extrapolating from his own lack of familiarity to others: "We are taught", "our minds", etc. I can easily construe some hypothetical person with…
Re: I Avoid Async/Await
#159Re: I Avoid Async/Await
#160Earlier quoted context omitted.
I don't think it's a good practice, atleast if you have typescript jn your stack. It's redundant and wraps the promise in another promise.
IIRC that doesn’t actually happen if a native Promise is returned.
async function foo(bar) {
// ...
}
function nonObviousAsyncFn() {
// ...
return foo(quux);
}
Explicit return types would help, but most people don’t write them unless they’re forced by a linter.