0

I have the following function that runs an interval internally until some work is complete:

doTasks() {

 let myInterval = setInterval(() => {

  // do some work

  if(workComplete === true) {
    clearInterval(myInterval);
  }

 },500);

}

How can i convert this function to return a resolved promise when the interval has been stopped?

DevMike
  • 1,630
  • 2
  • 19
  • 33
  • 1
    Hi! Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Dec 02 '19 at 07:59
  • 1
    *"How can i convert this function to return a resolved promise when the interval has been stopped?"* You wouldn't return a *resolved* promise. You'd return an unsettled promise that you later fulfill when calling `clearInterval`. – T.J. Crowder Dec 02 '19 at 08:00
  • I just realized that this is answered by [*How do I convert an existing callback API to promises?*](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises). – T.J. Crowder Dec 02 '19 at 08:01
  • 1
    It looks like the comment in which you said you were having trouble with the terminology was deleted, but I'll leave this here anyway. The terminology can be a bit confusing (particularly since people often misuse the word "resolve"). :-) The nutshell version: A promise starts out *unsettled* and (usually) gets *settled* at some point by being *fulfilled* or *rejected*. En route to being fulfilled or rejected (e.g., settled), promise A might be *resolved to* promise B, which means A will be fulfilled or rejected based on what happens to B. When you call `resolve(x)`, it either fulfills the... – T.J. Crowder Dec 02 '19 at 08:26
  • 1
    ...promise (if `x` is a non-promise value) or resolves it to a promise (if `x` is a promise). I'm fudging there by not distinguishing between *promise* and *thenable* because that would be too much all at once (arguably, the above already is). More in Chapter 8 of my new book if you like. :-) The [linked question's answers](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) basically show how to create and return the promise (which will be unsettled at that point) and then use `resolve` to fulfill it later. Happy coding! – T.J. Crowder Dec 02 '19 at 08:27
  • (The "people misuse the word 'resolve'" is **not** aimed at you. Just realized it could be read that way.) – T.J. Crowder Dec 02 '19 at 08:28
  • 1
    Yeah i deleted my comment thinking it just confused things even further unnecessarily. Thank you for all of this detail. Still researching. – DevMike Dec 03 '19 at 00:19

1 Answers1

2

Assuming that what you really want to is resolve the promise when interval is cleared, instead of returning it only then, you would do:

doTasks() {
    return new Promise(resolve => {
        let myInterval = setInterval(() => {
            if(workComplete === true) {
                clearInterval(myInterval);
                resolve()
            }
        },500);        
    })
}
Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30