I don't understand why the results display 'cat' first, followed by 'dog', and then 'bird'. I expect to see 'bird' first, followed by 'dog', and finally 'cat'.
async function asyncOperation1() {
return new Promise(resolve => {
let x = 0;
for (let i = 0; i < 10000000; i++) {
x += 1;
}
console.log(`cat`, x);
resolve(x)
});
}
async function asyncOperation2() {
return new Promise(resolve => {
let x = 0;
for (let i = 0; i < 1000; i++) {
x += 1;
}
console.log(`dog`, x);
resolve(x)
});
}
async function concurrentExecution() {
try {
asyncOperation1();
asyncOperation2();
console.log("bird");
} catch (error) {
console.error("An error occurred:", error);
}
}
concurrentExecution();
I am using Node.js version 18.19.0. I attempted to run the code, but the result did not meet my expectations, as I mentioned earlier.
add await in front of asyncOperation1 and asyncOperation2 i.e
await asyncOperation1(); await asyncOperation2();