I have an async function (a Promise) which does some things. I have to call it N times. Every call represents a simulation point. My first guess was to use a loop:
for(let i=0; i < N; i++) {
myAsyncFunc(data[i])
.then(() => myAsyncFunc(data[i]) )
}
Obviously, this does not work because the loops and before any subsequent call to myAsyncFun. How can I call step-by-step the async function, waiting for results and proceed to the next step?
I tried whit this:
function myAsyncFunc(data) {
return new Promise( (resolve, reject) => {
anotherAsync.then(resolve).catch(reject);
}
}
function simulate(mode) {
[...Array(10)].reduce((p, _, i) =>
p.then(_ => new Promise(resolve => {
myAsyncFunc(data[i]); // <== this return a Promise
}
))
, Promise.resolve());
}
But the functions myAsyncFunc are not called in sequence.