I have 2 methods that are almost identical except that one of them is async and returns a promise that resolves promises returned by the callback method and the other one isn't. The 2 functions are really identical, but because the callback is called in a loop, I don't really have any good idea on how to extract the common part while still waiting for the resolution of all promises. Any suggestions?
iterate(callback) {
let firstIteration = true;
let lastIp;
while (lastIp = (firstIteration && this.startingIp ? this.startingIp : this.subnet.nextIp(lastIp))) {
firstIteration = false;
callback(lastIp, this.subnet.buildProgressData(lastIp));
}
}
async iterateAsync(callback) {
let firstIteration = true;
let lastIp;
while (lastIp = (firstIteration && this.startingIp ? this.startingIp : this.subnet.nextIp(lastIp))) {
firstIteration = false;
await callback(lastIp, this.subnet.buildProgressData(lastIp));
}
}