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*/);
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);
});
}