First of all, I just wanted to say I'm a bit embarrassed to see people getting rude over what is clearly a legitimate, well-isolated, reproducible, and well explained issue. Shitting on other people's work like that is just plain unprofessional. I think whether funfix or bluebird were used as a POC solution should have been irrelevant, and that ideally people should have been eager (or at least open) to learn about the existence of a potential freaking crash and mitigation techniques rather than bickering over the definition of a memory leak.
So I guess the important question is: can the promise spec be fixed?
To be fair, there's nothing to fix. Yes you can optimize it, but then there has to be made a good argument as to why this is needed.
As it stands the issue sounds a lot like "I filled an array with the number 0 until JavaScript crashed (out of memory). This is bad, and if only the array was run-length encoded (compressed) to support an arbitrary number of 0's, there would not be a crash.
For both the original issue and my toy array example, yes you can optimize them, but the question becomes: Are there enough legitimate cases of this in the wild to warrant making the standard more complex? I think the answer is no, and there's plenty of room to create custom implementations outside of the spec to work around this issue (like the OP did).
Yes, I think there are enough legitimate use cases in the wild.
you cannot describe any functional programming algorithm involving loops without tail recursion — if folds are used, then folds are described with tail recursion as well
the issue is surprising — you, as a JS developer using JS's Promise may be familiar with it, but myself, being primarily a Scala / Haskell developer, I find this behavior really, really surprising, especially since it can be fixed
It's hard btw to explain use cases, as it would go over the scope of a single blog article and should be the subject of an entire book.
But I gave this example elsewhere ... processing really long / infinite streams of events, for which it's really natural to describe algorithms using tail recursions and for which you can't really work with imperative, mutation-based loops.
I gave this sample in another comment:
type Task<A> = () => Promise<A>
interface List<A>
class Cons<A> implements List<A> {
constructor(readonly head: A, readonly rest: Task<List<A>>) {}
}
class Nil implements List<A> {
constructor(readonly error?: any) {}
}
This data structure is really cheap and effective - being a lazy, asynchronous, referentially transparent stream. But as an exercise to the reader, try describing this function:
function map<A, B>(list: List<A>, f: (a: A) => B): List<B>
HINT: if that Promise implementation wouldn't leak, then it would be really easy to do.
I hear you, it definitely would be a nice perk, but you really can't be this focused on such a specific use case. To be able to change a specification safely, you need to consider all general cases.
Promise in JavaScript came from a need to be able to chain asynchronous operations, not to chain asynchronous streams of data into a program like you would in a fully functional language†. For example, look at the implementation of fetch which is a great example of what the ES standard Promise enables. Together with async/await you get to write synchronous-looking JavaScript while making things work in a single-threaded environment.
If you truly want this optimization to make it in, then a more even-sided argument should be made, not just "I can write code that works, so why doesn't the spec also work". For example, you reference the GitHub issue on the spec in your article, which shows just how much has to be considered in these cases. And it seems to me that only considers the case where a chain of promises completely wrap another promise (so you can eliminate the intermediate promises) whereas your example seems to be about a more complex relationship.
Honestly, I'm not sure the standard Promise can ever completely support what you're asking since there are interoperability concerns to consider (compatibility between different implementations of Promise, which the spec supports).
So to repeat, I think it's great you implemented a solution to your problem as someone who comes from a functional language. This will be helpful to others in the same situation, and you can also write an article about how you can write applications that operate on asynchronous streams with this library. But an article about changing the spec would require a much more general argument.
† Sidenote: I don't think the "surprising to Haskell users" argument is very relevant since the vast majority of JavaScript developers come from different backgrounds than functional programming. You will often find that adopting a new language requires you to think differently from whatever language you were using before.
I disagree about the need for this, because one use-case of going async is to escape the limitations of the call stack, but I understand from where the philosophy comes from. And I can't be really wrong, given that actual TCO is coming to JavaScript, being part of the ECMAScript 6 spec.
I do believe that FP is even more relevant in the context of JavaScript due to its asynchronous nature and thus potential for non-deterministic results (although less worrisome than on top of a platform that also does shared memory concurrency, like Java).
I also understand the concerns expressed by people on that issue. But there are several implementations in the wild that have been used successfully, at scale I might say, without users hitting gotchas, whereas such a memory leak can be a big gotcha.
Implementations include Scala's Future, in the wild since 2013 (which was my inspiration) and Twitter's Future, which they've been using within their whole infrastructure, in Finagle, etc. I deeply believe this is the right approach.
2
u/alexelcu Oct 11 '17
It depends on what your memory limit is and it doesn't crash immediately, first the GC freezes the world a few times, freeing whatever memory it can.