I'm trying to make a function call some code while allowing my main function to continue and not have to wait for it.
So far it's not working:
function doSomething() {
return new Promise((resolve, reject) => {
if(1) {
console.log("doing something");
for(let i = 0; i < 10000; i++) {
for(let j = 0; j < 10000; j++) {
for(let k = 0; k < 100; k++) {
// Do something for a long time to check if async is working
}
}
}
console.log("finished doing something");
resolve( {"dummydata": 10} );
}
else {
reject("error!");
}
})
}
function main() {
doSomething()
.then(data => console.log(data))
.catch(err => console.error(err))
console.log("this should print before doSomething() is finished");
}
main();
This is the output:
doing something
finished doing something
this should print before doSomething() is finished
{ dummydata: 10 }
Why is my program waiting for doSomething()? I want it to run in parallel / asynchronously. I also tried making main() an async function, same with doSomething(), but that didn't work.