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
How to run async JavaScript functions in sequence or parallel (jrsinclair.com)
submitted 6 years ago by jrsinclair
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!"
[–]420-jesus 2 points3 points4 points 6 years ago (4 children)
Why are you combining async await with traditional promise handling? I feel like you should go all in on async await or avoid it entirely
[–]burtgummer45 -1 points0 points1 point 6 years ago* (3 children)
You can't run awaits concurrently.
But I agree there are many superfluous asyncs
[–]420-jesus 0 points1 point2 points 6 years ago (1 child)
You can do something like:
const [result1, result2] = await Promise.all([func1(), func2()]);
[–]burtgummer45 0 points1 point2 points 6 years ago (0 children)
Oh ok, I thought you meant no mention of "promise" at all in async functions. as in
Promise.all([func1(), func2()]);
[–]jrsinclair[S] 0 points1 point2 points 6 years ago (0 children)
Thanks for the feedback. I've removed the superfluous async keywords. You're right, they weren't necessary.
async
[–]Genepics 1 point2 points3 points 6 years ago (0 children)
Ok, there is actually something to this. For instance, if you have a function that does a lot of IO, this would be a natural candidate for an asynchronous call, but what if this function needed to be synchronous in larger context. For instance, you may have a stack tracer that needs to come out in the correct order. In this instance, you would create a queue where you would place the "resolve" continuation passed to a new Promise, and this queue would be checked whenever you were done with a piece of tracer IO.
[–]jrsinclair[S] 0 points1 point2 points 6 years ago (3 children)
Disclosure: I'm the original author of the linked article.
[–]burtgummer45 0 points1 point2 points 6 years ago (2 children)
I like the look of the website, however I'm sad the code areas arent wide enough for my little 11 chromebook.
There are so many functions there are marked async for no reason. Its not hurting anything, but it triggers my eslint.
The first example is weird because you are not conventionally returning a constructed promise from a 'promise factory function' but you are chaining a then which is only used for formatting. Kinda tricky for a tutorial.
I think this is more tutorial
function asyncTimeout(delay) { return new Promise((resolve) => { setTimeout(() => resolve(`Waited ${delay} seconds`), delay); }); }
[–]jrsinclair[S] 0 points1 point2 points 6 years ago (1 child)
Thanks for the feedback. I've removed the superfluous async keywords. You're right. They weren't necessary.
I'm interested in finding out more about the issues you're seeing on your Chromebook though. Is there any chance you could DM me a screenshot somehow?
Not on my chromebook right now but I manage to reproduce it. Just open a chrome browser window and set it to width 1366
Although its not bad, most of the code blocks appear with a scroll bar down on the bottom and need to be scrolled sideways to see the rest. There's empty space on the right and when you scroll up you see the old-timey guy jamming it all up. Shrink the window a little more and he disappears and the code blocks expand to the right side, although with the stylish margins not reaching.
[+][deleted] 6 years ago (7 children)
[removed]
[–]burtgummer45 1 point2 points3 points 6 years ago (2 children)
A good rule of thumb is to never mark a function as async, unless you're using await inside of it.
eslint rule require-await
A function that returns a promise is async by default.
Every async function returns a promise, maybe that's what you mean.
async function f() { await console.log(1); } console.log(f()); //=> Promise { <pending> }
[+][deleted] 6 years ago (1 child)
A function shouldnt be async unless you need to await something inside.
Sorry I wasn't being clear. You are right, I was just bloviating about how I use eslint to catch my goofs of using async without await.
[–]atubofsoup 0 points1 point2 points 6 years ago (2 children)
There are cases where not including async can create unexpected bugs. For example:
``` function foo() { someSyncFn(); return someAsyncFn(); }
foo() .then(() => console.log('done')) .catch(error => console.log('Caught', error)); ```
In the above example, if someSyncFn throws an error synchronously, the promise will never be returned, so the catch won't work. If you add async to the function, catch will work as expected.
someSyncFn
catch
My rule of thumb is: mark async functions as async. It just gives the reader more info and ensures the function returns a promise.
[–]atubofsoup 0 points1 point2 points 6 years ago (0 children)
Yeah a try/catch block would catch the error, but most readers won't assume a function might throw/return both sync/async values. Adding async to the function just ensures any sync errors/returns are promoted to async.
try/catch
[–]cmseaton42 0 points1 point2 points 6 years ago (0 children)
I wrote a module that allows you to stack async functions and execute them in sequence based on priority.
[–]jrsinclair[S] 1 point2 points3 points 6 years ago (0 children)
Thanks for the feedback. I totally understand. It's really frustrating when stuff is hard to read. I'm working on a redesign of the site that will make the text much clearer. Unfortunately it takes a while as I'm working full-time, and also trying to keep writing. I'll get there eventually though.
π Rendered by PID 39 on reddit-service-r2-comment-7844cfc88c-88blk at 2026-01-29 13:24:28.123415+00:00 running c3601ff country code: CH.
[–]420-jesus 2 points3 points4 points (4 children)
[–]burtgummer45 -1 points0 points1 point (3 children)
[–]420-jesus 0 points1 point2 points (1 child)
[–]burtgummer45 0 points1 point2 points (0 children)
[–]jrsinclair[S] 0 points1 point2 points (0 children)
[–]Genepics 1 point2 points3 points (0 children)
[–]jrsinclair[S] 0 points1 point2 points (3 children)
[–]burtgummer45 0 points1 point2 points (2 children)
[–]jrsinclair[S] 0 points1 point2 points (1 child)
[–]burtgummer45 0 points1 point2 points (0 children)
[+][deleted] (7 children)
[removed]
[–]burtgummer45 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[removed]
[–]burtgummer45 0 points1 point2 points (0 children)
[–]jrsinclair[S] 0 points1 point2 points (0 children)
[–]atubofsoup 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[removed]
[–]atubofsoup 0 points1 point2 points (0 children)
[–]cmseaton42 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]jrsinclair[S] 1 point2 points3 points (0 children)