-1

I'm working on eventual consistency. In short I have a bunch of promises and I want to make sure they get retried until they succeed. It would make my life a lot easier if I could just persist the JavaScript promises I need to all succeed. That way, if one failed, I could try it again at a later time. However, I'm fairly certain that this is a frowned upon practice, and has a lot of issues with it that could be dangerous. Could someone explain to me why this is a bad idea?

2

Many functions cannot be easily persisted because they access variables of outside scopes (they are closures). A persistence mechanism would have to collect that context as well.

If the function may change this context, it won't be possible to persist the function+context in one process and restore it in another process, because the side effects of running the function would not be visible in the first process.

The core problem is that the code is not pure-functional but might have side effects. Eliminating the side effects would make a function safe to suspend, but JavaScript provides no language-level guarantee that this is indeed safe.

In your case, the solution is likely not to persist a function's context, but to encode the promises as events that can be explicitly stored: you can then retry to run some code until some output event is generated. Modelling something explicitly is often a sensible approach when the host language can't express some concept directly.

  • 4
    The Scala community played around with something similar: Spores – Safe Mobile Closures. It was in the context of distributed programming, so the idea was more about sending it to a different node than storing it for later, but the challenges are the same. So, you might get some ideas there. Note, however, that this a) required a change to the language, b) had the help of a sophisticated static type machinery, c) is close to PhD-level work, and d) didn't actually work out in the end. Doing it as a user-library in ECMASscript is going to be very hard. – Jörg W Mittag Mar 24 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.