It runs in this order because that's the order that the callbacks were entered into the callback queue.
Lets follow through what this code does:
Promise.resolve()
.then(() => console.log(2))
.then(() => console.log(3));
Promise.resolve()
.then(() => console.log(4))
.then(() => console.log(5));
console.log(1);
First, the main code is ran, two promises are created and resolved, and two callbacks are added to the callback queue; callbacks 2 and 4. The queue is now [2,4]. Then console.log(1) happens and the function ends.
Now that the function is done, the next callback in the callback queue runs, thus logging 2, that then pushes a new callback into the callback queue, callback 3. The queue is now [4,3].
Next, callback 4 runs and logs 4, and that pushes callback 5 into the queue. The queue is now [3,5]
Next, callbacks 3 and 5 each run in order, bringing the queue back to [].
If any of the functions were asynchronous, this order could have changed drastically based on how long each took to complete.