Reseting a promise?
See original GitHub issueI’ve come across the need to be able to reset a promise. That is, if the promise is resolved, I want to mark the promise as unresolved, then re-trigger the original call that created that promise, and have this re-trigger all the chain of then()
s that are attached to it.
It looks like promises are a one time object, and once resolved they get rid of all the attached callbacks and stay resolved forever. So the most obvious way to re-trigger the behavior is to throw away the old promise, create a new one, and attach all the then()
s again. However this does not work when you have multiple modules that use the promise to attach additional then()
s on it. Since the module where the promise is created has no knowledge of how many and how long are the then chains that have been attached, it cannot re-attach everything by itself.
This need has occurred in client-side applications where the promise objects are shared around across multiple modules that make use of the fetched data, often attaching then()
functions that do some further transformation. It would be very useful if I could persist a single promise object in memory for a long time, and be able to reset() it at the data layer without having to write additional code for all clients of that data layer so that they re-trigger (or re-attach) the then()
s to a new promise everytime the data is reloaded.
Issue Analytics
- State:
- Created 9 years ago
- Comments:8 (2 by maintainers)
Top GitHub Comments
You’re fundamentally failing to understand how promises work. You’re basically looking at a screw and asking for it to be changed into a Hammer because you tried to screw in a nail.
Promises will never be resettable. That violates one the invariants - that a promise is only resolved once.
What you are describing sounds to me like you want to create a pipeline, probably by using a Stream of some sort. Node.js/io.js has a Stream based on an Event Emitter, and it should work for what you want. I’ve yet to see an abstraction for pipelines in a similar vein of Promises being an abstraction for an asynchronous return value. The Stream code is itself “pure”, and there are other Stream implementations too, for client side code. Plus browserify.
@dtheodor I believe what you are looking for is a Signal. I wrote up a long essay about finding the right tool for the job which might be insightful. https://github.com/kriskowal/gtor
@havvy Thanks for your feedback. I find that people feel more welcome if you open by explaining how you understand their motivation.