Lately, I've been delving into JavaScript promises and stumbled upon a scenario that really intrigued me:
let combinedArray = [];
function getArrayOne() {
$http.post(arrayOnePath).then(function(arr) {
combinedArray = combinedArray.concat(arr);
}) // Additional code preventing the use of Promise.all(...)
}
function getArrayTwo() {
$http.post(arrayTwoPath).then(function(arr) {
combinedArray = combinedArray.concat(arr);
}) // More code preventing me from using Promise.all(...)
}
function getAllArrays() {
getArrayOne();
getArrayTwo();
}
As I was working on this logic, it occurred to me that there might be a potential race condition if both promises resolve simultaneously (due to them accessing a shared resource). Upon further contemplation, I realized that the then(..)
resolutions are triggered after the post request returns, indicating that this code operates within JavaScript's synchronous execution environment.
I'd appreciate some insight on whether the two instances of combinedArray.concat(arr);
could pose an issue if both promises are resolved concurrently.
[Edit]
In response to some of the feedback, I want to clarify that I'm not concerned about the order in which the arrays are concatenated into combinedArray
.