Hacker News new | ask | show | jobs
by WatchDog 677 days ago
> Async implicitly wraps the return in a promise

Yes. Without using await, it's basically the same thing as just changing `return result` to `return Promise.resolve(result)`.

> Useful for wrapping logic that otherwise would be synchronous.

The logic is still synchronous, except now the return value is just wrapped in a promise.

> Instead of using await you can instead just use “.then(() => part that needs to wait)”

I don't understand why this would ever be desirable versus just calling the function synchronously. If for some reason you really need `.then` I would just wrap the call to the function, instead of making the fn async, `Promise.resolve(myFn()).then(() => part that needs to wait)`

> if needing to wait on a set of async actions you have “Promise.all”.

You can pass non-promise values to `Promise.all`, and it will work fine, although I don't see why you would do it intentionally. ie `await Promise.all([1, Promise.resolve(2)])// resolves to [1,2]`.

> It is much more explicit on what part actually needs to wait

If anything it's less explicit, you are disguising the true behavior. You making a synchronous function look like it's async when it isn't.

It's also going to suffer a small performance penalty, although it shouldn't really matter unless the function is used in some tight loop. When I tested calling a very simple `return 1` function, making it async resulted in the benchmark taking twice as long.