-
Notifications
You must be signed in to change notification settings - Fork 396
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
base: master
Are you sure you want to change the base?
Conversation
Simple test program here: https://gist.github.com/briancavalier/f606b6af9b35855c092f |
var handler = this._handler; | ||
|
||
if (typeof handler.abort !== 'function') { | ||
return; |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
This looks great @briancavalier ! Thank you for taking this on.
It seems like if If closing over things as I did in my initial hack isn't an option, and I agree that it may not be due to performance reasons, then it may make more sense to evolve |
Yeah, and that'd be a bummer. The real issue is all the other useful operations around promises which aren't currently
... I'm starting to think that's actually the better option, too :) Then all promises (for now, all when.js promises) will have an |
For resolved promises, yes. But maybe unresolved promises should reject? |
I think vanilla I'd def like to have a go at another prototype where all promises have Please do feel free to hack on this branch if you have ideas :) |
DO NOT MERGE: This is a prototype of an AbortablePromise, for discussion and iteration to prove the idea.
This adds a new AbortablePromise subtype that has an
abort()
method which can send a signal from a consumer back toward the root of the promise graph to the producer indicating that the consumer wishes to abort the task that is computing the promise's value. This can be used to stop long-running or resource-consuming tasks when a consumer decides it no longer wants the result. For example: XHR request.Initial prototype
The initial implementation isn't ideal but is headed in the right direction. The abort functionality should be provided at the handler level, and a thin wrapper for it provided at the AbortablePromise.prototype level. The initial prototype does that, but by stashing an additional property on the existing handler. Ideally, we would have a new type of handler, eg AbortableHandler, that provided the functionality.
Open questions
This prototype doesn't provide
AbortablePromise.resolve
orAbortablePromise.reject
. I believe that it will need to. The reason is for API consistency: APIs that returnAbortablePromise
may need to return resolved and/or rejected promises, and returningPromise.resolve/reject
would return a promise without anabort()
method, which would break callers of those APIs.Promise.all
andPromise.race
may also need abortable analogs that return anAbortablePromise
. They would also need to handle abortable thenables in their input arrays, aborting them when the returned promise is aborted. That may be a slippery slope into needing to detectabort
in many more places (when.map, when.filter, when.reduce, etc etc etc).cc @mjackson