Supose that I have the following for loop, which would obviously block the event loop for a while:
function somethingExpensive() {
let i = 0;
while (true) {
// Something expensive...
if (++i === 1e10) return 'Some value.'
}
}
Can I wrap that operation (the for loop) inside a Promise, so it does not block the "main thread"?
Something like that:
function somethingExpensive() {
return new Promise((resolve) => {
let i = 0;
while (true) {
// Something expensive...
if (++i === 1e10) resolve('Some value.');
}
});
}