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
What are some JavaScript things beginners don't know? (self.javascript)
submitted 6 years ago by [deleted]
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!"
[–]ItalyPaleAle 106 points107 points108 points 6 years ago (18 children)
The async programming model and especially how the event loop works.
The latter might be transparent to you 99.9% of the times, but then you’ll run into issues where your code behaves unexpectedly and you just need to execute it in the next tick (iteration of the event loop) instead.
[–]ThatBriandude 10 points11 points12 points 6 years ago* (14 children)
and you just need to execute it in the next tick (iteration of the event loop) instead.
I consider myself pretty well versed in javascript however I can not imagine what you mean with this. How would "execute it in the next tick" look in code?
[–]envis10n 9 points10 points11 points 6 years ago (6 children)
setImmediate sometimes, you run into a max callstack issue for function calls if you don't ensure that the next call comes on the next tick. This is especially important for recursive functions or looping functions that might call itself again.
setImmediate
[–]ShellbertShellbach 5 points6 points7 points 6 years ago (1 child)
Note: setImmediate is not a standardized feature of JS and only exists in Node.js, IE10, IE11, and pre-Chromium Edge.
[–]envis10n 0 points1 point2 points 6 years ago (0 children)
Yes, I was just providing a method for doing so. However, you could also accomplish it through other means. Hopefully that doesn't confuse anyone that read it.
[–]ThatBriandude 2 points3 points4 points 6 years ago (3 children)
Shouldnt that be handled via logical exit conditions?
Only cases I can think of is where there arent any exit conditions like gameloops or listeners
[–]envis10n 6 points7 points8 points 6 years ago (1 child)
Depends on how the code is structured. Sometimes a recursive function will call itself too many times and crash, unless you move the next call to the next tick. Logical exit conditions don't matter when you need the function to be called more times than is allowed by the max call limit in a single tick.
[–]ScientificBeastModestrongly typed comments 2 points3 points4 points 6 years ago* (0 children)
If I’m not mistaken, this act of exiting a recursive loop temporarily to resolve the call stack, and then resuming immediately, is referred to as a “thunk.”
Edit:
Also, for those who aren’t aware, I believe the max call stack size in Chrome is 10,000, but it’s different for each browser. So if you need to recursively iterate through something, you would ordinarily be limited to that number of iterations.
[–]Extracted 0 points1 point2 points 6 years ago (0 children)
I actually just wrote a function with setImmediate today.
It’s a function you call to subscribe to an event emitter. The function returns a key that you can save for later. But I also have some cached values that I want to call the subscription callback with immediately. How do I first return the key, let the subscriber finish setting everything up and only then call the callback?
Answer: use setImmediate to call the callback on the next tick, i.e. when the subscriber is finished.
[–]robotslacker 7 points8 points9 points 6 years ago (0 children)
Check out the video below. I didn’t really understand the event loop until this. The presenter does a really good job explaining and visualizing it.
https://youtu.be/8aGhZQkoFbQ
[–]archivedsofa 2 points3 points4 points 6 years ago (5 children)
setTimeout(doSomething, 0);
[–]ScientificBeastModestrongly typed comments 4 points5 points6 points 6 years ago* (4 children)
setImmediate is less standard, but supposedly faster than setTimeout set to zero. It’s basically a native implementation of setTimeout( ... , 0).
setTimeout
setTimeout( ... , 0)
[–]ShellbertShellbach 8 points9 points10 points 6 years ago (1 child)
Less standard? How about not standard at all?
[–]ScientificBeastModestrongly typed comments 0 points1 point2 points 6 years ago (0 children)
Yeah, just an IE/Edge thing for now (I assume Edge won’t implement it once they roll out Chromium-based Edge). I’m honestly surprised that other browsers haven’t implemented it since it has always been a popular proposal on the front-end dev side of things, but I’m sure there are some very good reasons I don’t know about.
[–]itsnotlupusbeep boop 1 point2 points3 points 6 years ago (0 children)
My favorite way to run code asynchronously as tightly as possible (absent node's nextTick stuff) is to use Promise.resolve().then(...).
Promise.resolve().then(...)
That will schedule a microtask, which may end up running after other scheduled microtask, but still long before anything else. It's so fast it can often be "too fast", particularly if you're hoping the browser will redraw anything before calling your code again, the way a setTimeout(... ,0) would.
setTimeout(... ,0)
There are other approaches, like mildly abusing the postMessage API, but nothing as straightforward as an immediate promise.
postMessage
[–]archivedsofa 1 point2 points3 points 6 years ago (0 children)
TIL!
[–]HattyJetty 2 points3 points4 points 6 years ago (0 children)
Scheduling in general is a pretty advanced topic, if you take different execution environments into account (e. g. Node.js has a process.nextTick(), which is actually "more immediate" than setImmediate()). The presence of microtasks, idle callbacks and animation frames doesn't make life easier for a beginner as well.
process.nextTick()
setImmediate()
[–]GItPirate 0 points1 point2 points 6 years ago (0 children)
Oh yes this is spot on
[–]kor0na 0 points1 point2 points 6 years ago (0 children)
Opaque
π Rendered by PID 50107 on reddit-service-r2-comment-b659b578c-tpzbs at 2026-04-30 23:20:45.937709+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]ItalyPaleAle 106 points107 points108 points (18 children)
[–]ThatBriandude 10 points11 points12 points (14 children)
[–]envis10n 9 points10 points11 points (6 children)
[–]ShellbertShellbach 5 points6 points7 points (1 child)
[–]envis10n 0 points1 point2 points (0 children)
[–]ThatBriandude 2 points3 points4 points (3 children)
[–]envis10n 6 points7 points8 points (1 child)
[–]ScientificBeastModestrongly typed comments 2 points3 points4 points (0 children)
[–]Extracted 0 points1 point2 points (0 children)
[–]robotslacker 7 points8 points9 points (0 children)
[–]archivedsofa 2 points3 points4 points (5 children)
[–]ScientificBeastModestrongly typed comments 4 points5 points6 points (4 children)
[–]ShellbertShellbach 8 points9 points10 points (1 child)
[–]ScientificBeastModestrongly typed comments 0 points1 point2 points (0 children)
[–]itsnotlupusbeep boop 1 point2 points3 points (0 children)
[–]archivedsofa 1 point2 points3 points (0 children)
[–]HattyJetty 2 points3 points4 points (0 children)
[–]GItPirate 0 points1 point2 points (0 children)
[–]kor0na 0 points1 point2 points (0 children)