I have a function from a library that returns a promise. I need to run this function multiple times, but each iteration must wait until the previous task is done.
My assumption was that I could do this:
promiseReturner(1)
.then(promiseReturner(2)
.then(promiseReturner(3)
.then(...)
Which could be simplified using a loop:
var p = Promise.resolve();
for (var i=1; i<=10; i++) {
p = p.then(promiseReturner(i));
}
However, when I do this each promise in the chain is executed at the same time, instead of one after the other as .then()
seems to imply. Clearly I'm missing something fundamental about promises -- but after reading several tutorials and blog posts I'm still lost.