I have a function, that looks like this.
function () {
longArray.forEach( element => doSomethingResourceIntensive(element))
}
Because the array is long and the function is a little resource intensive, it freezes the browser.
Now I want to rewrite it using Promises, so it does the same thing, just not freezing the browser, and I want the solution to be elegant and "ES6-y"; ideally, the function would return Promise when all the iterations finished.
I found this question, where it's dealt with using setTimeout, but it seems a little "un-ES6-y", and it doesn't return a Promise.
I cannot do
function () {
return Promise.all(longArray.map( element =>
Promise.resolve().then(() => doSomethingResourceIntensive(element))
)
}
because I need to run the promises in succession and I am not sure if it would happen there.