Conversation
|
Simple test program here: https://gist.github.com/briancavalier/f606b6af9b35855c092f |
There was a problem hiding this comment.
Instead of returning undefined here, should we return a promise for the previous abortResult?
There was a problem hiding this comment.
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.resolveorAbortablePromise.reject. I believe that it will need to. The reason is for API consistency: APIs that returnAbortablePromisemay need to return resolved and/or rejected promises, and returningPromise.resolve/rejectwould return a promise without anabort()method, which would break callers of those APIs.Promise.allandPromise.racemay 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 detectabortin many more places (when.map, when.filter, when.reduce, etc etc etc).cc @mjackson