DEV Community

[Comment from a deleted post]
 
g1itcher profile image
G1itcher

There's a fair bit to unpack so apologies if I miss something.

Regarding database access using async/await in node: using Promise.all does not result in multiple threads (you can see this by sticking breakpoints in the methods; only one will run at a time). All you're doing with await is waiting for the asynchronous action that returns data to return its value before continuing. These two functions are functionally identical:

()=>{
  const foo = await bar();
  console.log(foo);
}

()=>{
  bar()
  .then(foo => {
    console.log(foo);
  });
}

Worth underlining that I explicitly said unrelated promises. If promises have interdependencies then yeah, they need to wait for each other.

Regarding your examples, the only way i could be changed during an await is if another callback has i in its scope and changes it. In which case, if you want i to change, you should be awaiting the promise that you expect to make that change. If you don't want i to change, don't await anything, the code is synchronous and you can safely run the rest of your code knowing that, even though you triggered some async functions, they won't resolve until the next event loop at the earliest.

If that misses your point I apologise. Do you have a more fleshed out example that we can see to try and clarify things?