Hello… I am coming from other programming languages and attempting to learn JS. One thing is bugging me regarding async/promises that I do not understand.
“`
function t1() {
console.log(‘In T1’);
for (let i = 0; i < 10000; i++) {
console.log(i);
}
return Promise.resolve(‘Finished T1’);
}
function t2() {
console.log(‘In T2’);
for (let i = 10000; i < 20000; i++) {
console.log(i);
}
return Promise.resolve(‘Finished T2’);
}
function main() {
t1().then((msg) => {
console.log(msg);
});
t2().then((msg) => {
console.log(msg);
});
console.log(‘R U N N I N G’);
}
main();
The above script produces an output that is like
“`
in T1
0
1
2
3
…
…
…
9997
9998
9999
in T2
10000
10001
10002
…
…
…
19997
19998
19999
R U N N I N G
Finished T1
Finished T2
However, I was expecting it to look more like
“`
in T1
in T2
R U N N I N G
(scattered/scrambled numbers here… could be anything between 0 < i < 10000 or 10000 < i < 20000
or the final Finished T1/Finished T2/RUNNING statements)
I am used to threading, and I was expecting the numbers to be out of order etc. since two tasks are running at the same time.
Also, I was expecting to see ‘In T1’ and ‘In T2’ and then ‘R U N N I N G’ to be the first lines printed to stdout.
But as the actual output shows, everything looks very synchronous and order is preserved. ‘R U N N I N G’ isn’t printed until both functions finish etc.
Then after rewriting and returning an array of the digits in each Promise… I started to get the results that I was expecting:
“`
function t1() {
let x = [];
console.log(‘In T1’);
for (let i = 0; i < 10000; i++) {
x.push(i);
}
return Promise.resolve(x);
}
function t2() {
let y = [];
console.log(‘In T2’);
for (let i = 10000; i < 20000; i++) {
y.push(i);
}
return Promise.resolve(y);
}
function main() {
// V1
t1().then((msg) => {
console.log(msg);
});
t2().then((msg) => {
console.log(msg);
});
console.log(‘R U N N I N G’);
// V2
// Promise.all([t1(), t2()]).then((msg) => {
// console.log(msg);
// });
}
main();
The output for the above script has the ‘In T1’, ‘In T2’, and ‘R U N N I N G’ messages showing first (I think the async is working… this is similar to what I was expecting to see in the first test):
“`
In T1
In T2
R U N N I N G
[
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99,
… 9900 more items
]
[
10000, 10001, 10002, 10003, 10004, 10005, 10006, 10007,
10008, 10009, 10010, 10011, 10012, 10013, 10014, 10015,
10016, 10017, 10018, 10019, 10020, 10021, 10022, 10023,
10024, 10025, 10026, 10027, 10028, 10029, 10030, 10031,
10032, 10033, 10034, 10035, 10036, 10037, 10038, 10039,
10040, 10041, 10042, 10043, 10044, 10045, 10046, 10047,
10048, 10049, 10050, 10051, 10052, 10053, 10054, 10055,
10056, 10057, 10058, 10059, 10060, 10061, 10062, 10063,
10064, 10065, 10066, 10067, 10068, 10069, 10070, 10071,
10072, 10073, 10074, 10075, 10076, 10077, 10078, 10079,
10080, 10081, 10082, 10083, 10084, 10085, 10086, 10087,
10088, 10089, 10090, 10091, 10092, 10093, 10094, 10095,
10096, 10097, 10098, 10099,
… 9900 more items
]
So why does the first example appear to be synchronous… ie. happening in order?