Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial prototype for AbortablePromise #415

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions lib/makeAbortablePromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/** @license MIT License (c) copyright 2010-2014 original author or authors */
/** @author Brian Cavalier */
/** @author John Hann */

(function(define) { 'use strict';
define(function() {

return function makeAbortablePromise(Promise) {

function AbortablePromise(resolver) {
Promise.call(this, makeAbortableResolver(resolver, this));
}

AbortablePromise.prototype = Object.create(Promise.prototype);

// Set constructor so that Promise.prototype.then will work
AbortablePromise.prototype.constructor = AbortablePromise;

AbortablePromise.prototype.then = function(f, r, p) {
var child = Promise.prototype.then.call(this, f, r, p);

// Propagate abort to child via handler
child._handler.abort = this._handler.abort;

return child;
};

AbortablePromise.prototype.abort = function() {
var handler = this._handler;

if (typeof handler.abort !== 'function') {
return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of returning undefined here, should we return a promise for the previous abortResult?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch. Either that, or maybe return Promise.resolve(this), ie a non-abortable view of the AbortablePromise instance?

}

// Detach abort so it can only be called once
var fn = handler.abort;
handler.abort = null;

try {
// aborter may return a promise to control the outcome of
// the abort process.
var abortResult = fn.apply(void 0, arguments);
handler.resolve(abortResult);

// Ensure return value is always a promise, specifically a
// non-abortable promise
return Promise.resolve(abortResult);
} catch (e) {
console.error(e.stack);
handler.reject(e);
}
};

function makeAbortableResolver(task, abortablePromise) {
return function() {
runAbortableTask(task, abortablePromise._handler);
};
}

function runAbortableTask(task, handler) {
// Make rejecting be the default abort behavior.
handler.abort = defaultAbort;

// Resolver may return an aborter function
var result = task(abortableResolve, abortableReject);

// If it did, stash it on the handler
if(typeof result === 'function') {
handler.abort = result;
}

function abortableResolve(x) {
// If x is also an abortable promise, propagate its abort function
// to handler.
handler.abort = getAbort(x);
handler.resolve(x);
}

function abortableReject(e) {
// Always disable abort upon reject
handler.abort = null;
handler.reject(e);
}

function defaultAbort(x) {
handler.reject(x);
}
}

return AbortablePromise;
};

function getAbort (x) {
// Make a reasonable attempt to recognize abortable thenables
return maybeThenable(x) ? x.abort : null;
}

function maybeThenable(x) {
return (typeof x === 'object' || typeof x === 'function') && x !== null;
}

});
}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }));
14 changes: 8 additions & 6 deletions lib/makePromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,27 @@ define(function() {
* @name Promise
*/
function Promise(resolver, handler) {
this._handler = resolver === Handler ? handler : init(resolver);
if(resolver === Handler) {
this._handler = handler;
} else {
this._handler = new Pending();
init(resolver, this._handler);
}
}

/**
* Run the supplied resolver
* @param resolver
* @param handler
* @returns {Pending}
*/
function init(resolver) {
var handler = new Pending();

function init(resolver, handler) {
try {
resolver(promiseResolve, promiseReject, promiseNotify);
} catch (e) {
promiseReject(e);
}

return handler;

/**
* Transition from pre-resolution state to post-resolution state, notifying
* all listeners of the ultimate fulfillment or rejection
Expand Down