Here is some code I am trying :
startTask();
function startTask(){
console.log('startTask enter');
startLongTask(1, function(){
console.log('long task complete');
});
console.log('startTask exit');
}
function startLongTask(i, callback) {
while (i++ < 10000){
console.log(i);
}
callback();
}
And here is the output (omitting the large series on numbers):
startTask enter
2
3
4
5
6
7
8
9
10
long task complete
startTask exit
As evident, if I have a long running operation, it's synchronous. It's like having startLongTask inline in startTask. How do I fix this callback to be non-blocking. The output I expect is :
startTask enter
startTask exit
2
3
4
5
6
7
8
9
10
long task complete