question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reseting a promise?

See original GitHub issue

I’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:closed
  • Created 9 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
Havvycommented, Mar 2, 2015

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.

5reactions
kriskowalcommented, Mar 2, 2015

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Restart a promise after fail - Stack Overflow
You need to wrap it in a function that you can call again. A promise by itself cannot be "restarted". var promise =...
Read more >
builtins.Promise.reset JavaScript and Node.js code examples
Best JavaScript code snippets using builtins.Promise.reset(Showing top 5 results out of 315) ; this.tableized = false; ; await super.reset(); ; async function ...
Read more >
Promise.race() - JavaScript - MDN Web Docs
The Promise.race() method takes an iterable of promises as input and returns a single Promise. This returned promise settles with the ...
Read more >
Restoring Promise | Restoring Promise
Restoring Promise is an initiative creating housing units grounded in dignity for young adults in prison.
Read more >
@promises/reset - npm
Reset is package from Promises library. Latest version: 0.5.0, last published: 4 years ago. Start using @promises/reset in your project by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found