Skip to content

Instantly share code, notes, and snippets.

@neall
Created April 29, 2014 15:09
Show Gist options
  • Save neall/11403197 to your computer and use it in GitHub Desktop.
Save neall/11403197 to your computer and use it in GitHub Desktop.
I wanted to make a promise-wrapped setTimeout that had a way to abort. I am not 100% happy with just giving the promise an abort() method.
promiseTimeout = (delay) ->
extraMethods = {}
promise = new Promise (resolve, reject) ->
handle = setTimeout(resolve, delay)
extraMethods.abort = (reason = new Error('timeout aborted')) ->
if handle
clearTimeout(handle)
reject(reason)
handle = false
promise.abort = extraMethods.abort
promise
@neall
Copy link
Author

neall commented Apr 29, 2014

Also, that extraMethods variable seems hackish - is there a better way to do that in CoffeeScript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment