Skip to content

Instantly share code, notes, and snippets.

@mjackson
Created December 3, 2014 23:24
Show Gist options
  • Save mjackson/2dabe596c881644e8311 to your computer and use it in GitHub Desktop.
Save mjackson/2dabe596c881644e8311 to your computer and use it in GitHub Desktop.
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