Ok, that's so cool but... what about clearTimeout?
Because I'm sure many have thought about transforming setTimeout into a promise (e.g. was common in AngularJS), but there's always a catch.
There has been a debate about cancelable promises a couple of years ago, we got nothing it out of it 😕
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
This is a fantastic question. Without cancellable promises, you would not be able to cancel the setTimeout.
For a majority of use cases cancelling a promise is not needed. Though that doesn't mean it is not a valid use case. Throttling and debouncing is an example of a valid use case when you would want to cancel a promise.
The sleep implementation I created currently does not support cancellation. I have created an issue for this here: github.com/joelnet/MojiScript/issu.... This is a feature that would be great to have.
Here's a very naive implementation that could support cancellation:
constsleep=milliseconds=>value=>{lettimeoutIdconstcancel=()=>clearTimeout(timeoutId)constpromise=newPromise(resolve=>{timeoutId=setTimeout(()=>resolve(value),milliseconds)})returnObject.assign(promise,{cancel})}(async()=>{// create promise and cancelconstpromise=sleep(1000)('Uh oh!')const{cancel}=promise// promise to log when resolvedpromise.then(console.log)// cancel promisecancel()console.log('Done.')})()
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:
constctrl=newAbortController()constsignal=ctrl.signalfetch(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...
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Ok, that's so cool but... what about
clearTimeout
?Because I'm sure many have thought about transforming
setTimeout
into a promise (e.g. was common in AngularJS), but there's always a catch.There has been a debate about cancelable promises a couple of years ago, we got nothing it out of it 😕
This is a fantastic question. Without cancellable promises, you would not be able to cancel the
setTimeout
.For a majority of use cases cancelling a promise is not needed. Though that doesn't mean it is not a valid use case. Throttling and debouncing is an example of a valid use case when you would want to cancel a promise.
The
sleep
implementation I created currently does not support cancellation. I have created an issue for this here: github.com/joelnet/MojiScript/issu.... This is a feature that would be great to have.Here's a very naive implementation that could support cancellation:
Bluebird also supports promise cancellation: bluebirdjs.com/docs/api/cancellati...
Converting this
Promise
to use bluebird'sPromise
would also work.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 anAbortController
object:You can extend your
sleep
function to accept asignal
option and listen to anabort
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 anAbortController
. Maybe I'll create adebounce
to compare the differences.Definitely something to think about.