Let say I have this module :
export async function beFancy () {
let i, j, load;
// just for trying to delay the return;
for (i = 0; i < 999; ++i) {
for (j = 0; j < 999; ++j) {
load = i + j;
}
}
return 'beFancy finished';
}
now I make a main module for testing :
import {beFancy} from './mymodule';
beFancy().then((msg) => console.log(msg));
console.log('main finished');
If I execute, the output is as expected
-> main finished
-> beFancy finished
because beFancy is -asynchronous-
But now if I try to make the loop a little more intense :
export async function beFancy() {
let i, j, load;
for (i = 0; i < 9999999; ++i) {
for (j = 0; j < 999; ++j) {
load = i + j;
}
}
return 'beFancy finished';
}
and executing main again, the output is the same
-> main finished
-> beFancy finished
but I would expect main finished to display before the function beFancy is actually being processed. Instead the above output displays in one shot when beFancy finish. At first I thought the reason was because the output is only flushed at the end of the program but if I type :
console.log('begin');
beFancy().then((msg) => console.log(msg));
console.log('main finished');
"begin" displays before 'beFancy' executes, so my previous assumption is not met.
So because beFancy is -asynchronous- what could possibly be wrong ? Is it an unexpected behavior on my machine ?