fantasyland / fantasy-land

Specification for interoperability of common algebraic structures in JavaScript
MIT License
10.12k stars 377 forks source link

Ordering of parallel `chain`s #12

Closed Twisol closed 9 years ago

Twisol commented 11 years ago

Sourcing from a when.js issue:

p.chain(f).chain(g);
p.chain(h);

In the presence of side-effects, the order of execution matters. If the order is FHG (i.e. breadth-first), and h has a side-effect, then g may be affected by a totally separate chain (and indeed, p.chain(h) could be in an entirely different part of the file).

We could stick with precedent and push the concern downstream, but I want to at least raise the issue as something that should be considered somewhere along the line.

jneen commented 10 years ago

Is .chain mutating p in your example? It seems like when.js is actually launching IO operations when .then is called, which is not really part of the spec. A truly monadic promise would just return another promise, and you'd have to call .run() or something on your application to actually launch it.

SimonRichardson commented 10 years ago

Agreed, we should make a note in the spec that performing IO operations with in a chain should return another promise, or it's up to the user to manage.

bergus commented 10 years ago

In promises-aplus it was figured out that the order of g and h is unspecified - which especially makes sense if f is asynchronous so that it runs concurrently. Assuming the functions are pure it doesn't make a difference anyway, and in case they have side-effects and somehow depend on each other their order should be stated explicitly in the code if that is required.

SimonRichardson commented 9 years ago

Each function should ideally be referential transparent and with lack of side effects. So I'm closing this as a part of the unspecified part of the specification.

Reopen if you disagree :8ball:

puffnfresh commented 9 years ago

@SimonRichardson :+1: