Created
December 3, 2014 23:24
-
-
Save mjackson/2dabe596c881644e8311 to your computer and use it in GitHub Desktop.
This file contains 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
var promise = new AbortablePromise(function (resolve, reject, onAbort) { | |
// Use resolve & reject as you normally would. | |
var request = makeRequest( ... , function (error, response) { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(response); | |
} | |
}); | |
// Use onAbort to register a promise.abort() function. It is the | |
// responsibility of this function to abort the execution of the | |
// promise and resolve/reject as needed. | |
onAbort(function () { | |
request.abort(); | |
reject(new Error('Request was aborted')); | |
}); | |
}); | |
promise.abort(); // Calls the onAbort handler. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment