Hacker News new | past | comments | ask | show | jobs | submit login

My problem with promises is that you can't "uninvoke" them. I.e., if all listeners unsubscribe, the original job they were waiting for could be killed, but the design of Promises doesn't allow that. Instead, the job will continue to be executed (without any listeners), and thus wasting resources.



I won't argue that sometimes you might want to cancel a promise that you no longer need (ex: user cancels ajax operation) but just because there are no listeners does not always mean you don't want it to complete (ex: Fire and forget, not always a great idea sure but a thing nonetheless).


True, but you can implement fire and forget by adding a dummy listener. The functionality I'm suggesting is more general.


I think it's less of a design flaw and more a conceptual issue. Since a Promise is just a value, it can be stored in an arbitrary data structure on the heap with no listeners. But the then() method can be called any time later. So, a promise being potentially "listened to" is really just being reachable by the garbage collector.

There's no reason the source of the promise can't implement a cancel() method, though.


But a cancel() method would not be a "clean" solution if there potentially are multiple listeners.

I really want to compute things if and only if there are listeners.


Why are you generating the promise in the first place if there are no listeners? Why don't you return a function which can be used to generate the promise instead?


I'm not sure what would be the advantage.

The number of listeners can go up and down during the computation. When that number reaches zero, the computation needs to be stopped.

When the number becomes nonzero again, the computation needs to be restarted.

Those are the requirements, basically.


For that use case, maybe an event emitter would work better?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: