re: setTimeout is a callback-style function. What would happen if we change that? VIEW POST

TOP OF THREAD FULL DISCUSSION
re: This is a fantastic question. Without cancellable promises, you would not be able to cancel the setTimeout. For a majority of use cases cancelling...
 

Yes, that could work! 🙌

You can also consider the approach that's been taken for cancelling a fetch promise: it's based on a "signal" that's being created by an AbortController object:

const ctrl = new AbortController()
const signal = ctrl.signal
fetch(someUrl, { signal })
  .catch(error => {
    console.log(error.name)
  })

// Later on...
ctrl.abort()
// Then it logs 'AbortError'

You can extend your sleep function to accept a signal option and listen to an abort event. Although it does seem a little overkill for something like this...

This is interesting. I'll have to create some test project with the .cancel and an AbortController. Maybe I'll create a debounce to compare the differences.

Definitely something to think about.

code of conduct - report abuse