Created
April 29, 2014 15:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, that extraMethods variable seems hackish - is there a better way to do that in CoffeeScript?