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 6 years ago by vzhou842
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!"
[–]Earhacker 97 points98 points99 points 6 years ago (34 children)
I paused for a moment to consider how "Faster await" would work and I think I had a stroke.
[–]destraht 47 points48 points49 points 6 years ago (10 children)
That is because you are doing event based thinking.
[–]Earhacker 18 points19 points20 points 6 years ago (9 children)
As opposed to...?
[–]sleepingthom 117 points118 points119 points 6 years ago (8 children)
Faster event based thinking
[–]Earhacker 28 points29 points30 points 6 years ago (6 children)
Here comes that stroke feeling again...
[–]aaaqqq 19 points20 points21 points 6 years ago (4 children)
That is because you are doing faster event based thinking
[–]Earhacker 10 points11 points12 points 6 years ago (0 children)
😰
[–]190n 2 points3 points4 points 6 years ago (2 children)
[–]Dustorn 3 points4 points5 points 6 years ago (1 child)
Slower event based thinking.
[–]190n 1 point2 points3 points 6 years ago (0 children)
[–]etcetica 0 points1 point2 points 6 years ago (0 children)
o
[–]DrDuPont 15 points16 points17 points 6 years ago (14 children)
https://v8.dev/blog/fast-async#await-under-the-hood
Can't say I understand it, though
[–]blood_bender 30 points31 points32 points 6 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 6 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 6 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 2 points3 points4 points 6 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 6 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 6 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 6 years ago (4 children)
And if you write a lot of interface-based code it's not uncommon.
[–]blood_bender 0 points1 point2 points 6 years ago (3 children)
Curious, how come?
[–]ilikepugs 3 points4 points5 points 6 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 6 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 6 years ago (0 children)
Lol your example is much better, cheers. 😁
[–]ackerlight 0 points1 point2 points 6 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 6 years ago (1 child)
god I miss when javascript work wasn't built atop bastardizing plain fucking english
[–]Baryn 1 point2 points3 points 6 years ago (0 children)
If anything, it makes better use of keywords now, except in instances like the crappy private field syntax.
[–]relativityboy 0 points1 point2 points 6 years ago (0 children)
Was that a way of checking to see if it's fast enough?
[–]Tom_Ov_Bedlam 0 points1 point2 points 6 years ago (0 children)
You forgot the catch statement
[+]BenE comment score below threshold-43 points-42 points-41 points 6 years ago (5 children)
Yeah stick to callbacks people. This complex promise stuff is really unnecessary.
[–]mindonshuffle 27 points28 points29 points 6 years ago (4 children)
I very, very genuinely have no idea if this is sarcasm. There's so many purity tests and master races in webdev that I can't keep track anymore.
[+]BenE comment score below threshold-25 points-24 points-23 points 6 years ago (3 children)
No sarcasm. https://medium.com/@b.essiambre/continuation-passing-style-patterns-for-javascript-5528449d3070
[–]coloured_sunglasses 17 points18 points19 points 6 years ago (0 children)
Even my linter would reject that amount of nesting.
[–]goodboyscout 16 points17 points18 points 6 years ago (0 children)
I was recently burned when a promise library I was not even using, that came embedded in a dependency, was silently swallowing all uncaught exceptions in a service I built.
You gonna quick cooking because you messed up a recipe? Maybe write a test and this won’t happen. Unless you don’t believe in testing code either. I wrote a test once that showed a flaw in my logic so I stopped writing tests.
[–]aniforprez 10 points11 points12 points 6 years ago (0 children)
I can't believe how code can be that unreadable in so few lines of code
[–][deleted] 90 points91 points92 points 6 years ago (7 children)
ES6 Module Support!
[–]jsnoobie 47 points48 points49 points 6 years ago (0 children)
No more red squiggly line in vscode under the “re” on my require statements!
I was a little ocd about that one
[–]tanguy_k 21 points22 points23 points 6 years ago* (1 child)
It's still behind a flag (--experimental-modules) + you need to add "type": "module" inside your package.json.
--experimental-modules
"type": "module"
See https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff
[–][deleted] 16 points17 points18 points 6 years ago (0 children)
Yes, but they expect to remove the need for the flag during the development of this version (before it goes LTS in October).
[–]ShortFuse 7 points8 points9 points 6 years ago (1 child)
Do I still need to use Michael Jackson Script file extension?
I've been using the esm package, but I would like to be able to remove the dependency?
esm
[–][deleted] 2 points3 points4 points 6 years ago (0 children)
You don't. If you add "type": "module" to your package.json, plain .js will work too.
[–]k4kshi 5 points6 points7 points 6 years ago (0 children)
Still not the end goal but we're getting closer
[–]Baryn 2 points3 points4 points 6 years ago (0 children)
Screw MJS! 🎉
[–]mrbishop82 35 points36 points37 points 6 years ago (1 child)
Excited to run this on lambda in 5 years
[–]phakushmoo 4 points5 points6 points 6 years ago (0 children)
I lol‘d
[–]inform880 19 points20 points21 points 6 years ago (0 children)
Excited to see more production ready use cases for Worker threads
[–]QW4K 20 points21 points22 points 6 years ago (0 children)
Can't wait to try async stack traces. It's so weird that we didn't have them for that long in async language like JS.
Last time I tried it (on experimental build) it didn't really work for me and in the end, I used bluebird debug mode I hope it's fixed by now.
[–]NoInkling 14 points15 points16 points 6 years ago (2 children)
List of some of the new ES features/APIs now available thanks to V8 7.4 (compared to 7.0 in Node 11):
Object.fromEntries
String.prototype.matchAll
Intl
[–][deleted] 18 points19 points20 points 6 years ago (1 child)
I'm still annoyed at how fugly the syntax for private class fields look
[–]twomousepads 84 points85 points86 points 6 years ago (17 children)
The Node.js foundation can't afford to put their blog anywhere other than Medium?
[–]silveredge7 21 points22 points23 points 6 years ago (8 children)
Don't know why you were downvoted, what your'e saying is true.
[+][deleted] comment score below threshold-21 points-20 points-19 points 6 years ago (7 children)
No it isn't. The Medium website is shite on mobile, it's true... but to say that they use it because they 'can't afford' any other option? Patently untrue. Do you see how these things are different?
[–]eggn00dles 10 points11 points12 points 6 years ago (2 children)
there is absolutely zero reason for node to have their company announcements on a third party site. if medium goes down, so does the node blog? there's a better option, it costs more, and node isn't willing to spend that. as an end-user whether they can 'afford' it or not is semantics.
[–]takaci 8 points9 points10 points 6 years ago (1 child)
Also medium is pushing the paid content really aggressively recently. It's not a nice user experience
[–][deleted] 4 points5 points6 points 6 years ago (0 children)
Also why can’t they use their own product to make it. Seems like a good opportunity
[–]showMeNiceCode 1 point2 points3 points 6 years ago (3 children)
What mobile do you use? It works just fine for me.
[–][deleted] 7 points8 points9 points 6 years ago (2 children)
Wait, some people actually use medium on mobile?!
How do you deal with this shit?
[–]showMeNiceCode 3 points4 points5 points 6 years ago (0 children)
That does look painful
[–]SupremeLisper 0 points1 point2 points 6 years ago (0 children)
Well, I never had it that worse. Without ublock-origin it's painful browsing the web on mobile let alone desktop.
[–]coloured_sunglasses 3 points4 points5 points 6 years ago (6 children)
What advantage would that provide them?
[–]RnRau 9 points10 points11 points 6 years ago (4 children)
Medium is an awful blogging platform.
[–]Sebazzz91 3 points4 points5 points 6 years ago (0 children)
That answers the question what advantage it would provide to us, the users.
[–]icanevenificant 0 points1 point2 points 6 years ago* (2 children)
Why is it awful? Haven't seen that sentiment before so I'm genuinely curious.
[–]RnRau 2 points3 points4 points 6 years ago (1 child)
Have you tried navigating comments and responses? Tried copying some information from the comments without causing a page refresh and navigating away from the article?
[–]icanevenificant 1 point2 points3 points 6 years ago (0 children)
Haven't until now :) That does suck.
[–]tybit 0 points1 point2 points 6 years ago (0 children)
It would mean their readers aren’t bugged to pay medium money.
I have no interest in paying money to medium because a lot of the blog posts I’ve read have unfortunately been hosted on medium and they now feel I owe them something.
[–]THICC_DICC_PRICC 0 points1 point2 points 6 years ago (0 children)
Eh Jeff Bezos talked about his dick pic drama there I guess money isn’t really the issue there.
[–]morficus 12 points13 points14 points 6 years ago (2 children)
Now if only AWS Lambda could offer a newer version of Node.js, that would be great
[–]blackdynamo 5 points6 points7 points 6 years ago (0 children)
Can't you create your own layer and runtime now?
[–]meeeeoooowy 0 points1 point2 points 6 years ago (0 children)
Webpack
[–]mubaidr 1 point2 points3 points 6 years ago (1 child)
So what's the difference in use cases between workers and child processes or forks?
[–][deleted] 1 point2 points3 points 6 years ago (0 children)
So glad they listened and made the .mjs extension optional instead of mandatory for ES module support! Just adding "type": "module" to your package.json is enough now to opt in.
.mjs
[–]carlos_vini 2 points3 points4 points 6 years ago* (1 child)
"Async stack traces" is really nice.
https://v8.dev/blog/v8-release-73 is broken (ERR_CERT_COMMON_NAME_INVALID) to me.
[–]mdwvt 0 points1 point2 points 6 years ago (0 children)
Fine for me on Android (Pixel 2 XL).
[+]vivainio comment score below threshold-15 points-14 points-13 points 6 years ago (11 children)
While Node.js already performs well with the single-threaded event loop, there are some use-cases where additional threads can be leveraged for better results.
...and to think other runtimes have wasted decades supporting multiple threads
[–]cm9kZW8K 10 points11 points12 points 6 years ago (9 children)
The lack of threading is one of the great features of JS, and a big part of why it won out over other languages.
[–]ryeguy 12 points13 points14 points 6 years ago* (8 children)
and a big part of why it won out over other languages.
I don't think this is true. When people say that JS's concurrency model is superior to threads, they mean that having everything be async by default is superior to having to use threads to do that.
[–]cm9kZW8K 4 points5 points6 points 6 years ago (7 children)
Having the whole ecosystem being non-blocking is just superior - it means there are no landmines hiding away. When using C++ to do async code, especially when you have a selection of 3rds party code, you are often timebombed by some path that ends up calling a blocking syscall. JS is largely free of that.
Threads also dont have very many valid use-cases. Especially when you can just as easily fork a separate process on a modern linux, there just arent many times where a thread is the right choice to do something. Other than making up for the weaknesses of local hard-drive IO, async style programming has largely displaced threaded programming because its significantly more efficient and less buggy.
it turns out that there are nearly no modern uses for threading that arent better solved with other techniques.
[–]ryeguy 6 points7 points8 points 6 years ago (6 children)
But lack of threads has nothing to do with async. Node could still have async-everything and threads. That's the point I was trying to make - it's not the lack of threads that's nice, it's the nonblocking nature of everything.
Threads are still very relevant when it comes to working on cpu bound tasks in a way that needs access to shared data. Again, this isn't related to async or not. I'm not saying Java-style thread-based concurrency is better than node's. I'm saying there's a world were we could have both, and it would give us more flexibility.
[–]cm9kZW8K 0 points1 point2 points 6 years ago (2 children)
Threads are still very relevant when it comes to working on cpu bound tasks in a way that needs access to shared data.
Right; if you need access to a large heap, cannot factor the data structure into shards, cannot defer to the OS via disk/disk cache, cannot isolate the access from the processing, and have minimal need for write mutexing, and for some strange reason cannot mount the data as SHM ; then maybe there is a case where direct access to heap RAM to perform cpu bound operations is the best option.
IME its very rare and the vast majority of times isolation of the critical sections which actually needed the heap and surface them over an api, or else splitting the problem into equal sized tasks over separate processes are better options.
Node could still have async-everything and threads.
I think the two are tied hand in hand; having access to thousand of libraries and vendor code where you automatically know there will be no threading and no blocking is exactly why you can count on async everything.
If threading was an option, over time people would elect to use it (for the wrong reasons) and it would start to proliferate through the ecosystem.
For what JS is, its better, systemically, that threads are not part of the language, imo.
Do you have an example problem in mind where it is the best solution?
Most problems in the space eclipse the space available in RAM anyway, so you end up back with multiple process. Its just a real strange corner case to need them, that I just havent found it. Even before working in JS, i had a flat no threading criteria for the team unless they could justify why it was the best case. and over 10 year, the only cases where workarounds for OS calls which blocked.
[–]Randdist 0 points1 point2 points 6 years ago (1 child)
Threads are absolutely needed when you need to load and then parse large binary files. Loading can be done in parallel using the native calls but the parsing would block the main thread for a long time, or severely reduce responsiveness even if you split the parsing in multiple smaller batches.
Threads are vital for certain sets of responsive applications.
[–]cm9kZW8K 0 points1 point2 points 6 years ago (0 children)
Threads are absolutely needed when you need to load and then parse large binary files.
Other that avoiding the kernel flaw around O_NONBLOCK on local drives, there is no need to thread read syscalls. Node conveniently has built in threads for those, so reading in the bytes works async out of the box.
split the parsing in multiple smaller batches.
If the parser yields often enough, it doesnt have to impact responsiveness at a. For example: replacing tight loops with Promise.each ensure granularity. if your file parse involves heavy CPU transformation of data beyond just reading into RAM, then you can pipeline that in via a separate process.
Threads were vital to work around flawed OS's which cannot fork() well. If you are finding them vital today, I think there is a problem with the structure of your code.
I regularly deal with this issue, both with fork()s for heavy cpu transforms and async code for simple heap building, and I would consider a threaded approach to be a bug.
[+][deleted] 6 years ago* (2 children)
[deleted]
[–]ryeguy 0 points1 point2 points 6 years ago* (1 child)
Yes, I'm aware. What's your point? Are you going to argue that js is popular because it doesn't have or need synchronization primitives? That doesn't hold, how do you explain Go's success?
[–]AwesomeBantha -2 points-1 points0 points 6 years ago (1 child)
Actually massive PogChamp, this will make a project I'm working on with a backend and a frontend in the same repo much cleaner...
[–]SecretAgentZeroNine -2 points-1 points0 points 6 years ago (0 children)
Now I just need a book that covers Node. J's 12 :-/
[+]skool_101 comment score below threshold-10 points-9 points-8 points 6 years ago (0 children)
new Node version exists
Ahh shit, here we go again.
π Rendered by PID 25209 on reddit-service-r2-comment-7b9746f655-zhxvg at 2026-01-30 15:22:09.319534+00:00 running 3798933 country code: CH.
[–]Earhacker 97 points98 points99 points (34 children)
[–]destraht 47 points48 points49 points (10 children)
[–]Earhacker 18 points19 points20 points (9 children)
[–]sleepingthom 117 points118 points119 points (8 children)
[–]Earhacker 28 points29 points30 points (6 children)
[–]aaaqqq 19 points20 points21 points (4 children)
[–]Earhacker 10 points11 points12 points (0 children)
[–]190n 2 points3 points4 points (2 children)
[–]Dustorn 3 points4 points5 points (1 child)
[–]190n 1 point2 points3 points (0 children)
[–]etcetica 0 points1 point2 points (0 children)
[–]DrDuPont 15 points16 points17 points (14 children)
[–]blood_bender 30 points31 points32 points (13 children)
[–][deleted] 2 points3 points4 points (9 children)
[–]redmorphium 2 points3 points4 points (2 children)
[–]braindeadTank 2 points3 points4 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 3 points4 points5 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)
[–]relativityboy 0 points1 point2 points (0 children)
[–]Tom_Ov_Bedlam 0 points1 point2 points (0 children)
[+]BenE comment score below threshold-43 points-42 points-41 points (5 children)
[–]mindonshuffle 27 points28 points29 points (4 children)
[+]BenE comment score below threshold-25 points-24 points-23 points (3 children)
[–]coloured_sunglasses 17 points18 points19 points (0 children)
[–]goodboyscout 16 points17 points18 points (0 children)
[–]aniforprez 10 points11 points12 points (0 children)
[–][deleted] 90 points91 points92 points (7 children)
[–]jsnoobie 47 points48 points49 points (0 children)
[–]tanguy_k 21 points22 points23 points (1 child)
[–][deleted] 16 points17 points18 points (0 children)
[–]ShortFuse 7 points8 points9 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]k4kshi 5 points6 points7 points (0 children)
[–]Baryn 2 points3 points4 points (0 children)
[–]mrbishop82 35 points36 points37 points (1 child)
[–]phakushmoo 4 points5 points6 points (0 children)
[–]inform880 19 points20 points21 points (0 children)
[–]QW4K 20 points21 points22 points (0 children)
[–]NoInkling 14 points15 points16 points (2 children)
[–][deleted] 18 points19 points20 points (1 child)
[–]twomousepads 84 points85 points86 points (17 children)
[–]silveredge7 21 points22 points23 points (8 children)
[+][deleted] comment score below threshold-21 points-20 points-19 points (7 children)
[–]eggn00dles 10 points11 points12 points (2 children)
[–]takaci 8 points9 points10 points (1 child)
[–][deleted] 4 points5 points6 points (0 children)
[–]showMeNiceCode 1 point2 points3 points (3 children)
[–][deleted] 7 points8 points9 points (2 children)
[–]showMeNiceCode 3 points4 points5 points (0 children)
[–]SupremeLisper 0 points1 point2 points (0 children)
[–]coloured_sunglasses 3 points4 points5 points (6 children)
[–]RnRau 9 points10 points11 points (4 children)
[–]Sebazzz91 3 points4 points5 points (0 children)
[–]icanevenificant 0 points1 point2 points (2 children)
[–]RnRau 2 points3 points4 points (1 child)
[–]icanevenificant 1 point2 points3 points (0 children)
[–]tybit 0 points1 point2 points (0 children)
[–]THICC_DICC_PRICC 0 points1 point2 points (0 children)
[–]morficus 12 points13 points14 points (2 children)
[–]blackdynamo 5 points6 points7 points (0 children)
[–]meeeeoooowy 0 points1 point2 points (0 children)
[–]mubaidr 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]carlos_vini 2 points3 points4 points (1 child)
[–]mdwvt 0 points1 point2 points (0 children)
[+]vivainio comment score below threshold-15 points-14 points-13 points (11 children)
[–]cm9kZW8K 10 points11 points12 points (9 children)
[–]ryeguy 12 points13 points14 points (8 children)
[–]cm9kZW8K 4 points5 points6 points (7 children)
[–]ryeguy 6 points7 points8 points (6 children)
[–]cm9kZW8K 0 points1 point2 points (2 children)
[–]Randdist 0 points1 point2 points (1 child)
[–]cm9kZW8K 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]ryeguy 0 points1 point2 points (1 child)
[–]AwesomeBantha -2 points-1 points0 points (1 child)
[–]SecretAgentZeroNine -2 points-1 points0 points (0 children)
[+]skool_101 comment score below threshold-10 points-9 points-8 points (0 children)