Вы можете использовать для ожидания ...
const veg = ["carrot", "tomatoes", "potatos", "celery", "pepper"];
const combo = [false, true, true, true, true]; // 4 out of 5
const t0 = performance.now();
function display(){
const t1 = performance.now();
console.log(filter(veg, combo), ` time = ${t1-t0}`);
}
async function* nextPerm(a){
do {
await new Promise(res => setTimeout(res, 1000));
yield a;
} while(nextPermutation(a));
}
let g = nextPerm(combo);
(async function() {
for await (let next_combo of g) {
display();
}
})();
function filter(a, select) {
return a.filter((_e,i) => select[i]);
}
function nextPermutation(array, first = 0, last = array.length-1) {
if(first>=last){
return false;
}
let i = last;
for(;;){
const i1 = i;
if(array[--i]<array[i1]){
let i2 = last+1;
while(array[i]>=array[--i2]);
[array[i], array[i2]] = [array[i2], array[i]];
reverse(array, i1, last);
return true;
}
if(i===first){
reverse(array, first, last);
return false;
}
}
}
function reverse(array, i=0, j=array.length-1) {
while (i < j)
[array[i++], array[j--]] = [array[j], array[i]];
}