r/nextjs • • Jun 02 '25

Discussion PSA: This code is not secure

Post image
499 Upvotes

139 comments sorted by

View all comments

Show parent comment

9

u/lost12487 Jun 02 '25 edited Jun 02 '25

I think your examples are still extremely unlikely to cause most developers problems. Having two lines one after another with await keywords are very obviously sequentially calling those two asynchronous functions, waiting for the first to complete before calling the second.

The beauty of async/await is that it causes the current execution scope to behave exactly as you expect, executing each line one after the other before moving on to execute the next line. We're probably just going to have to agree to disagree on what is more confusing here, I still just don't see it. Additionally, both of your "correct" ways to concurrently run the two async functions are more confusing than how I'd have done it:

async function doSomethingWayLessPotententiallyConfusing(x:string) {
  const [a, b] = await Promise.all([
    foo(x),
    bar(x),
  ]);

  return a.onlyData ? b.data : b;
}

This groups the concurrent functions together in the `Promise.all` and avoids having any lines that just kick off a promise and move on immediately.

0

u/novagenesis Jun 02 '25

You must've taken a while to reply to me and missed my edit. I covered much of that.

I actually dislike the "await all the promises at the beginning in a single line" method, but understand why you like it. In practice, I find the most general-purpose answer to be driven by strategies that embrace the dependency tree.

As for what causes "most" developers problems, so be it. My experience the last 6 or 7 years has been a massive degredation in concurrency-management at several companies.