Below is a simple recursive function that takes in a length, and decrements it using setTimeout.  Once the length is <= 0, it's done.
How can I write this function (in pure JavaScript) so that I can use it like so:
animate(999).then(...)
const animate = length => {
  console.log(length)
  length -= 10
  if (length <= 0) {
    length = 0
    return
  }
  setTimeout(() => {animate(length)}, 10)
}
animate(999)Update:
This is what I've tried.  The problem I'm having is that it seems resolve is either not being called, or is being called on a different promise.
const animate = length => {
  return new Promise((resolve, reject) => {
    console.log(length)
    length -= 10
    if (length <= 0) {
      length = 0
      resolve(true)
      return // without this the function runs forever
    }
    setTimeout(() => {animate(length)}, 10)
  })
}
animate(999).then(result => console.log(result))** Working Update (but don't understand it) **
const animate = length => {
  return new Promise((resolve, reject) => {
    console.log(length)
    length -= 10
    if (length <= 0) {
      length = 0
      return resolve(true)
    }
    setTimeout(() => {resolve(animate(length))}, 10)
  })
}
animate(999).then(result => console.log(result)) 
     
     
     
     
    