Since I’ve been mucking about with JavaScript more, I’ve discovered that for some reason the Promise pattern is a profoundly unintuitive one to me. In my quest to accurately model their semantics in my mind, I’ve read through the A+ spec and written a couple of partial Promise implementations of my own. I still don’t feel like I’ve completely nailed it down.
Here is an incomplete list of my misunderstandings (so far) about Promises.
- Promises are not boxes that values appear in.
- Promises are not really about values at all.
- Promises are also not about the work.
- Or about managing work.
- They are a signaling mechanism off to the side of the work.
- Promises aren’t about giving you something to wait on.
- You don’t know when a Promise will be fulfilled; you only get to say what should happen if and when it’s fulfilled.
- A Promise is not a pure data structure. A complete implementation requires a deferral mechanism (e.g. a way to kick TODOs into a global reactor next-tick queue).
- Promises are not about pulling. They are about pushing.
- Promises will not make your async code visualizable as a nice clean dataflow diagram (look to actors and channels for that).
- Promises are terribly named. They give no assurance about anything. (Cynical readers might argue that this makes them exactly like real-world promises).
- Promises do not manage or perform work. A chain of promises:
- Signals when new work can be started.
- Channels results from one operation to the next.
- Of the two, only the signaling part is essential. The channeling part can be and is often unused.
- .then() is not an operation on a Promise. .then() is a specialized constructor of new Promises.
- Promises are causality insertions.
Your link to cohere is broken.