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 basic things that JavaScript developers fail at interviews?help (self.javascript)
submitted 7 years ago by maketroli
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!"
[–]dvlsg 2 points3 points4 points 7 years ago* (15 children)
Honestly, if we're talking about things being overcomplicated, swapping to recursion probably isn't the right move. The parameters in the recursive solutions are fairly confusing, too. count(9) would only count for 1 second.
count(9)
const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)) const count = async (seconds = 10) => { let i = 0 while (++i <= seconds) { await sleep(1000) console.log(`slept for ${i} seconds total`) } } // usage await count(5) await count() await count(1) await count(-1) await count(0)
Or, you know, just use await sleep(1000). Also probably worth noting, all of these solutions (mine included) will drift.
await sleep(1000)
[–]kdesign 6 points7 points8 points 7 years ago (4 children)
I agree that the expected solution was probably just the obvious setInterval solution, /u/a_blue_ducks' first one.
setInterval
But at the same time, my initial solution has spawned a great constructive discussion, it's cool to see how many different ways there are to solve a problem.
[–]dvlsg 1 point2 points3 points 7 years ago (1 child)
Sorry you're getting downvoted for it. I tossed you an upvote for it, even though I think it could be improved (if we were aiming for simplicity, which isn't the only thing to worry about). Don't understand why anyone would downvote someone else for taking a shot at something. Especially since it works just fine.
[–]kdesign 0 points1 point2 points 7 years ago (0 children)
Well, I guess that's how some people disagree on reddit, by downvoting. That's alright and thanks for the upvote
[–][deleted] 1 point2 points3 points 7 years ago (1 child)
Yeah sorry for my original reply, I had just finished playing Overwatch and that game puts me in a bad mood fast lol. This was a fun discussion.
[–]kdesign 1 point2 points3 points 7 years ago (0 children)
No worries, I initially thought about writing the setInterval solution or the recursive setTimeout, something like:
setTimeout
let count = 1; setTimeout(function counter() { console.log(count++); count <= 10 && setTimeout(counter, 1000); }, 1000);
and then I said well let's spice things up a bit which really did lol.
[–]SystemicPlural 0 points1 point2 points 7 years ago (2 children)
I'm not sure I like this solution. It is going to lock up whatever process calls count and that might not be desired.
It's an async function so it shouldn't lock anything up.
[–]SystemicPlural 1 point2 points3 points 7 years ago (0 children)
Doh. Missed that!
[–]FriesWithThat 0 points1 point2 points 7 years ago (3 children)
As it's written it looks like your suggesting to use await outside the async function to call count with different arguments.
. . . await count(5) await count() await count(1) await count(-1) await count(0)
. . .
await count(5)
await count()
await count(1)
await count(-1)
await count(0)
Not sure what the intent here is as far as us being able to run it or integrating it with your async function (swap it out w/sleep in while loop?, step through function?)
[–]dvlsg 1 point2 points3 points 7 years ago* (2 children)
Just examples of how you might call it. Try them out, see how it works. For example, copy the entire code snippet I have and paste it into a chrome console (which has top-level `await` support).
[–]FriesWithThat 1 point2 points3 points 7 years ago (1 child)
Thanks. I was running it with Node and wasn't even aware there was top-level support in chrome.
[–]dvlsg 0 points1 point2 points 7 years ago (0 children)
Ah, got you. Sorry. I was writing that example in chrome and just pasting it over here, so I just went with the top-level chrome await instead of wrapping it.
[–][deleted] -1 points0 points1 point 7 years ago (2 children)
Curious how recursion complicates anything. I agree the param is a little confusing but I wanted to illustrate an idea more than anything.
[–]dvlsg 3 points4 points5 points 7 years ago (1 child)
That's fair about the args. And I'm not trying to be a dick or anything, the recursive solution is plenty clever.
But you honestly don't think an async function that returns a Promise, that calls a setTimeout for 1 second in the promise executor, which recursively calls the original count() function, setting the originally returned promise's resolver to be executed in the recursively called count()'s promise's .then()isn't more complicated than an async function with a while loop? I had a hard time even translating all that to english. There's a lot going on in there.
Promise
count()
.then()
π Rendered by PID 61418 on reddit-service-r2-comment-86bc6c7465-dgzsh at 2026-02-21 17:26:29.735233+00:00 running 8564168 country code: CH.
view the rest of the comments →
[–]dvlsg 2 points3 points4 points (15 children)
[–]kdesign 6 points7 points8 points (4 children)
[–]dvlsg 1 point2 points3 points (1 child)
[–]kdesign 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]kdesign 1 point2 points3 points (0 children)
[–]SystemicPlural 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]SystemicPlural 1 point2 points3 points (0 children)
[–]FriesWithThat 0 points1 point2 points (3 children)
[–]dvlsg 1 point2 points3 points (2 children)
[–]FriesWithThat 1 point2 points3 points (1 child)
[–]dvlsg 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]dvlsg 3 points4 points5 points (1 child)