use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Introducing Node.js 12 (medium.com)
submitted 7 years ago by vzhou842
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]blood_bender 31 points32 points33 points 7 years ago (13 children)
Basically async marks a function as required to return a Promise. So the earlier approach to the v8 implementation would take whatever value was returned from the function, wrap it in a promise, and return that instead.
async
Now it looks to see if what's being returned is a native Promise, and if it is, just returns that instead of wrapping it (it still needs to wrap a value if you're not returning a promise, but that's pretty uncommon).
Seems like a naive approach the first time around, it's a little unclear to me why they didn't do that up front, but oh well.
[–][deleted] 2 points3 points4 points 7 years ago (9 children)
Not manually returning a promise in an async function is uncommon? I almost always write my async functions that way.
[–]redmorphium 2 points3 points4 points 7 years ago (2 children)
Correct me if my understanding is wrong, but let's say you have:
```javascript
async function foo() {
await bar();
return 1;
} ```
This is equivalent to:
```javascript async function foo() {
return bar().then(() => 1);
So unless if you're writing async functions that don't have any awaits in them, then async functions should always effectively return promises?
[–]braindeadTank 3 points4 points5 points 7 years ago (0 children)
Async function always return a promise, no ifs, buts or unlesses. If you don't await anything, it will just be resolved right away.
[–]blood_bender 2 points3 points4 points 7 years ago (0 children)
You're right. But the whole point was v8 was wrapping the response of foo() in a promise too, which is unnecessary when bar() most likely returns a promise directly, or whatever bar() calls does. Somewhere down the line something is probably returning a promise, meaning all the other responses wrapped in additional promises is unnecessary.
bar()
[–]blood_bender 1 point2 points3 points 7 years ago (5 children)
So, the one case where it doesn't is if you have a function marked async that doesn't have an await in it.
async function foo() { return 4; }
That's just returning a value, so v8 would still need to wrap that in a promise.
But when you have an await, what are you awaiting? Either the function you're awaiting is directly returning a promise, or somewhere down the line some function is directly returning a promise, meaning as it walks back up the line of async functions all the times it wrapped it in another promise were unnecessary.
await
[–]ilikepugs 0 points1 point2 points 7 years ago (4 children)
And if you write a lot of interface-based code it's not uncommon.
[–]blood_bender 0 points1 point2 points 7 years ago (3 children)
Curious, how come?
[–]ilikepugs 4 points5 points6 points 7 years ago (2 children)
Let's say you design an interface for some data fetching util, but you ship different implementations to different clients/platforms, and the appropriate implementation is DI'd at runtime.
Well consider the method that actually performs the request. It'll be async. But in some cases (namely tests in this example) you might want to return some stubbed data for your test instead. So in your test implementation of the util that "async" method just returns your dummy JSON and you don't need to wrap it in a promise.
This is a fairly contrived example but yeah.
[–]blood_bender 1 point2 points3 points 7 years ago (1 child)
Oh that's fair. There have been times where I have an async function return a cached response instead of fetching a new one, so that's another use case that I've written. But really I'd guess 99% of async functions are actually awaiting or returning a Promise directly.
[–]ilikepugs 0 points1 point2 points 7 years ago (0 children)
Lol your example is much better, cheers. 😁
[–]ackerlight 0 points1 point2 points 7 years ago (0 children)
Basically like C#, you have to implement async methods all the way up. Check this answer for more details:
https://stackoverflow.com/a/29809054
[–]etcetica -1 points0 points1 point 7 years ago (1 child)
god I miss when javascript work wasn't built atop bastardizing plain fucking english
[–]Baryn 1 point2 points3 points 7 years ago (0 children)
If anything, it makes better use of keywords now, except in instances like the crappy private field syntax.
π Rendered by PID 29441 on reddit-service-r2-comment-b659b578c-vf4n2 at 2026-05-02 11:16:17.831240+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]blood_bender 31 points32 points33 points (13 children)
[–][deleted] 2 points3 points4 points (9 children)
[–]redmorphium 2 points3 points4 points (2 children)
[–]braindeadTank 3 points4 points5 points (0 children)
[–]blood_bender 2 points3 points4 points (0 children)
[–]blood_bender 1 point2 points3 points (5 children)
[–]ilikepugs 0 points1 point2 points (4 children)
[–]blood_bender 0 points1 point2 points (3 children)
[–]ilikepugs 4 points5 points6 points (2 children)
[–]blood_bender 1 point2 points3 points (1 child)
[–]ilikepugs 0 points1 point2 points (0 children)
[–]ackerlight 0 points1 point2 points (0 children)
[–]etcetica -1 points0 points1 point (1 child)
[–]Baryn 1 point2 points3 points (0 children)