'Trouble promisifying setInterval to do an action continuously for a given period and then allow a new asynchronous function to begin

Having trouble promisifying setInterval. The .then() is occurring immediately despite the embedEditor function returning a promise only if time === 0. How do I make a function in setInterval return a promise once a time period has completed?

const interval = setInterval(embedEditor, 1000);
async function embedEditor() {
    time -= 1;
    //inner function....//
    if (time === 0) {
        clearInterval(interval);
        return new Promise((res) => { res("time period over"); });
    }
}
await interaction.reply({ embeds: [exampleEmbed], components: [row, row2] });
await interval
    .then(/*action*/);


Solution 1:[1]

Answer from Fannie Zemlak that works like a charm:

const asyncInterval = async (callback, ms, triesLeft = 5) => {
  return new Promise((resolve, reject) => {
    const interval = setInterval(async () => {
      if (await callback()) {
        resolve();
        clearInterval(interval);
      } else if (triesLeft <= 1) {
        reject();
        clearInterval(interval);
      }
      triesLeft--;
    }, ms);
  });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tempnameenjoyer