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:
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?
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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:
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 anawait
is if another callback hasi
in its scope and changes it. In which case, if you wanti
to change, you should beawait
ing the promise that you expect to make that change. If you don't wanti
to change, don'tawait
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?